package com.bci.pwtz.service.cad;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.ConfigMapper;
import com.bci.pwtz.mysql.dao.DybxMapper;
import com.bci.pwtz.mysql.dao.DydlMapper;
import com.bci.pwtz.mysql.dao.DydlfjxMapper;
import com.bci.pwtz.mysql.dao.DydlzdMapper;
import com.bci.pwtz.mysql.dao.DydxMapper;
import com.bci.pwtz.mysql.dao.DygtMapper;
import com.bci.pwtz.mysql.dao.DyjckyMapper;
import com.bci.pwtz.mysql.dao.DykgMapper;
import com.bci.pwtz.mysql.dao.DykggMapper;
import com.bci.pwtz.mysql.dao.DymxMapper;
import com.bci.pwtz.mysql.dao.DypdpMapper;
import com.bci.pwtz.mysql.dao.DypdxMapper;
import com.bci.pwtz.mysql.dao.DyrsxMapper;
import com.bci.pwtz.mysql.dao.DysydlbhqMapper;
import com.bci.pwtz.mysql.dao.DywgbczzMapper;
import com.bci.pwtz.mysql.dao.DyxlMapper;
import com.bci.pwtz.mysql.dao.DyyhjrdMapper;
import com.bci.pwtz.mysql.dao.DyzrdqMapper;
import com.bci.pwtz.mysql.dao.DyzsblqMapper;
import com.bci.pwtz.mysql.dao.DyzsdrqMapper;
import com.bci.pwtz.mysql.dao.DyzskgMapper;
import com.bci.pwtz.mysql.dao.FgdzzMapper;
import com.bci.pwtz.mysql.dao.PdbyqMapper;
import com.bci.pwtz.mysql.dao.ZsbyqMapper;
import com.bci.pwtz.mysql.dao.ZygtMapper;
import com.bci.pwtz.mysql.model.Config;
import com.bci.pwtz.mysql.model.Dybx;
import com.bci.pwtz.mysql.model.Dydl;
import com.bci.pwtz.mysql.model.Dydlfjx;
import com.bci.pwtz.mysql.model.Dydx;
import com.bci.pwtz.mysql.model.Dygt;
import com.bci.pwtz.mysql.model.Dypdx;
import com.bci.pwtz.mysql.model.Dyrsx;
import com.bci.pwtz.mysql.model.Dysydlbhq;
import com.bci.pwtz.mysql.model.Dywgbczz;
import com.bci.pwtz.mysql.model.Dyxl;
import com.bci.pwtz.mysql.model.Dyzrdq;
import com.bci.pwtz.mysql.model.Dyzsblq;
import com.bci.pwtz.mysql.model.Dyzsdrq;
import com.bci.pwtz.mysql.model.Dyzskg;
import com.bci.pwtz.mysql.model.Pdbyq;
import com.bci.pwtz.mysql.model.Zsbyq;
import com.bci.pwtz.service.BusinessCommonService;
@Service
public class Cad_Oper {
	private ZygtMapper zygtMapper;	 //
	private ZsbyqMapper zsbyqMapper;  //柱上变压器   T_ZSBYQ
	private PdbyqMapper pdbyqMapper;  //配电变压器 T_PDBYQ  
	private DyxlMapper dyxlMapper;	 //低压线路 T_DYXL
	
	private DygtMapper dygtMapper;	 //低压杆塔（关联低压杆塔和站房设备）  T_DYGT 
	private DyzskgMapper dyzskgMapper; //低压柱上开关     T_DYZSKG	
	private DyzrdqMapper dyzrdqMapper; //低压柱熔断器     T_DYZRDQ	
	private DyrsxMapper dyrsxMapper; //低压柱上熔丝箱  T_DYRSX	
	private DyzsdrqMapper dyzsdrqMapper; //低压柱上电容器  T_DYZSDRQ	
	private DywgbczzMapper dywgbczzMapper; //低压柱上无功补偿装置  T_DYWGBCZZ	
	private DyzsblqMapper dyzsblqMapper; //低压柱上避雷器 T_DYZSBLQ	

	private DydxMapper dydxMapper; //低压导线（关联起始杆塔） T_DYDX
	private DysydlbhqMapper dysydlbhqMapper; //低压剩余电流保护器  T_DYSYDLBHQ	
	private DybxMapper    dybxMapper   ;  //低压表箱  T_DYBX		                                       
	
	//以下2个是低压站房设备,从属低压线路; 关联低压杆塔和站房设备.
	private DypdxMapper   dypdxMapper  ;  //低压配电箱  T_DYPDX 		                                   
	private DydlfjxMapper dydlfjxMapper;  //低压电缆分接箱 T_DYDLFJX	

	//以下5个是站内一次设备，从属以上2个站房设备		
	private DykgMapper    dykgMapper   ;  //低压开关 T_DYKG	                                         
	private DymxMapper    dymxMapper   ;  //低压母线 T_DYMX	                                         
	private DypdpMapper   dypdpMapper  ;  //低压配电盘 T_DYPDP	                                       
	private DykggMapper   dykggMapper  ;  //低压开关柜 T_DYKGG	                                       
	private FgdzzMapper   fgdzzMapper  ;  //反孤岛装置 T_FGDZZ	                                       
	
	private DydlMapper    dydlMapper   ;  //低压电缆（关联起始杆塔和站房） T_DYDL		                 
	private DydlzdMapper  dydlzdMapper ;  //低压电缆终端 T_DYDLZD	                                   
	
	private DyyhjrdMapper dyyhjrdMapper;  //低压用户接入点 T_DYYHJRD		                               
	private DyjckyMapper  dyjckyMapper ;  //低压交叉跨越（关联杆塔和站房，有图片属性)）T_DYJCKY		   
	
	private ConfigMapper configMapper;
	
	private BusinessCommonService businessCommonService;
	
	public static int LOG_DEBUG = 0;
	public static int LOG_INFO = 1;
    private Logger logger=Logger.getLogger(Cad_Oper.class);

//	public static final String[] Table_names = { 
//	    "低压线路", "低压导线", "低压杆塔", "低压柱上开关", "低压柱上熔断器", 
//	    "低压熔丝", "低压柱上电容器", "低压无功补偿装置", "低压柱上避雷器", "低压表箱", 
//	    "低压熔丝箱", "低压剩余电流保护器", "低压电缆段", "低压电缆终端", "低压电缆接头", 
//	    "低压配电箱", "低压电缆分接箱", "低压开关", "低压母线", "低压配电盘", 
//	    "低压开关柜", "反孤岛装置", "变压器", "低压间隔单元", "低压墙支架", 
//	    "用户接入点", "缺陷清理", "" };
//	
    //"低压柱上开关", "低压柱上熔断器","低压柱上电容器", "低压无功补偿装置", "低压柱上避雷器", "低压熔丝箱",
	public static final int[] subDeviceOfDYGT = { 3, 4, 6, 7, 8, 10 };
	public static final int byqTableIndex = 22;
	public static final int dydlTableIndex = 12;
	public static final int dydxTableIndex = 1;
	public static final int dygtTableIndex = 2;
	public static final String[] otlay = { "表箱", "剩余电流保护器", "配电箱", "分接箱" };
	//{"低压表箱", "型号"} {"低压剩余电流保护器","维护班组"} {"投运日期"}{"设备主人"} 
	public static final int[][] otsub = { { 9, 9 }, { 11, 6 }, { 15, 7 },{ 16, 8 } };
	public Long companyId = 0L;
	public LinkedHashMap<String, String> byqNameList;
	public LinkedHashMap<String, Zsbyq> zsbyqList ;
	public LinkedHashMap<String, Pdbyq> pdbyqList ;
	public ArrayList<Cad_Rect> m_subTptRectList;
	public Cad_Rect tmp_byqjwd = null; //经纬度
	public int max_press = 0;
	public String oper_byq = "";
	public int oper_press = 0;

	
	public ZygtMapper getZygtMapper() {
		return zygtMapper;
	}

	public void setZygtMapper(ZygtMapper zygtMapper) {
		this.zygtMapper = zygtMapper;
	}

	public ZsbyqMapper getZsbyqMapper() {
		return zsbyqMapper;
	}

	public void setZsbyqMapper(ZsbyqMapper zsbyqMapper) {
		this.zsbyqMapper = zsbyqMapper;
	}

	public PdbyqMapper getPdbyqMapper() {
		return pdbyqMapper;
	}

	public void setPdbyqMapper(PdbyqMapper pdbyqMapper) {
		this.pdbyqMapper = pdbyqMapper;
	}

	public DyxlMapper getDyxlMapper() {
		return dyxlMapper;
	}

	public void setDyxlMapper(DyxlMapper dyxlMapper) {
		this.dyxlMapper = dyxlMapper;
	}

	public DygtMapper getDygtMapper() {
		return dygtMapper;
	}

	public void setDygtMapper(DygtMapper dygtMapper) {
		this.dygtMapper = dygtMapper;
	}

	public DyzskgMapper getDyzskgMapper() {
		return dyzskgMapper;
	}

	public void setDyzskgMapper(DyzskgMapper dyzskgMapper) {
		this.dyzskgMapper = dyzskgMapper;
	}

	public DyzrdqMapper getDyzrdqMapper() {
		return dyzrdqMapper;
	}

	public void setDyzrdqMapper(DyzrdqMapper dyzrdqMapper) {
		this.dyzrdqMapper = dyzrdqMapper;
	}

	public DyrsxMapper getDyrsxMapper() {
		return dyrsxMapper;
	}

	public void setDyrsxMapper(DyrsxMapper dyrsxMapper) {
		this.dyrsxMapper = dyrsxMapper;
	}

	public DyzsdrqMapper getDyzsdrqMapper() {
		return dyzsdrqMapper;
	}

	public void setDyzsdrqMapper(DyzsdrqMapper dyzsdrqMapper) {
		this.dyzsdrqMapper = dyzsdrqMapper;
	}

	public DywgbczzMapper getDywgbczzMapper() {
		return dywgbczzMapper;
	}

	public void setDywgbczzMapper(DywgbczzMapper dywgbczzMapper) {
		this.dywgbczzMapper = dywgbczzMapper;
	}

	public DyzsblqMapper getDyzsblqMapper() {
		return dyzsblqMapper;
	}

	public void setDyzsblqMapper(DyzsblqMapper dyzsblqMapper) {
		this.dyzsblqMapper = dyzsblqMapper;
	}

	public DydxMapper getDydxMapper() {
		return dydxMapper;
	}

	public void setDydxMapper(DydxMapper dydxMapper) {
		this.dydxMapper = dydxMapper;
	}

	public DysydlbhqMapper getDysydlbhqMapper() {
		return dysydlbhqMapper;
	}

	public void setDysydlbhqMapper(DysydlbhqMapper dysydlbhqMapper) {
		this.dysydlbhqMapper = dysydlbhqMapper;
	}

	public DybxMapper getDybxMapper() {
		return dybxMapper;
	}

	public void setDybxMapper(DybxMapper dybxMapper) {
		this.dybxMapper = dybxMapper;
	}

	public DypdxMapper getDypdxMapper() {
		return dypdxMapper;
	}

	public void setDypdxMapper(DypdxMapper dypdxMapper) {
		this.dypdxMapper = dypdxMapper;
	}

	public DydlfjxMapper getDydlfjxMapper() {
		return dydlfjxMapper;
	}

	public void setDydlfjxMapper(DydlfjxMapper dydlfjxMapper) {
		this.dydlfjxMapper = dydlfjxMapper;
	}

	public DykgMapper getDykgMapper() {
		return dykgMapper;
	}

	public void setDykgMapper(DykgMapper dykgMapper) {
		this.dykgMapper = dykgMapper;
	}

	public DymxMapper getDymxMapper() {
		return dymxMapper;
	}

	public void setDymxMapper(DymxMapper dymxMapper) {
		this.dymxMapper = dymxMapper;
	}

	public DypdpMapper getDypdpMapper() {
		return dypdpMapper;
	}

	public void setDypdpMapper(DypdpMapper dypdpMapper) {
		this.dypdpMapper = dypdpMapper;
	}

	public DykggMapper getDykggMapper() {
		return dykggMapper;
	}

	public void setDykggMapper(DykggMapper dykggMapper) {
		this.dykggMapper = dykggMapper;
	}

	public FgdzzMapper getFgdzzMapper() {
		return fgdzzMapper;
	}

	public void setFgdzzMapper(FgdzzMapper fgdzzMapper) {
		this.fgdzzMapper = fgdzzMapper;
	}

	public DydlMapper getDydlMapper() {
		return dydlMapper;
	}

	public void setDydlMapper(DydlMapper dydlMapper) {
		this.dydlMapper = dydlMapper;
	}

	public DydlzdMapper getDydlzdMapper() {
		return dydlzdMapper;
	}

	public void setDydlzdMapper(DydlzdMapper dydlzdMapper) {
		this.dydlzdMapper = dydlzdMapper;
	}

	public DyyhjrdMapper getDyyhjrdMapper() {
		return dyyhjrdMapper;
	}

	public void setDyyhjrdMapper(DyyhjrdMapper dyyhjrdMapper) {
		this.dyyhjrdMapper = dyyhjrdMapper;
	}

	public DyjckyMapper getDyjckyMapper() {
		return dyjckyMapper;
	}

	public void setDyjckyMapper(DyjckyMapper dyjckyMapper) {
		this.dyjckyMapper = dyjckyMapper;
	}
	

	public ConfigMapper getConfigMapper() {
		return configMapper;
	}

	public void setConfigMapper(ConfigMapper configMapper) {
		this.configMapper = configMapper;
	}
	
	public BusinessCommonService getBusinessCommonService() {
		return businessCommonService;
	}

	public void setBusinessCommonService(BusinessCommonService businessCommonService) {
		this.businessCommonService = businessCommonService;
	}

	public void Log(int type, String info){
		switch (type) {
		case 0 :
			if (logger.isDebugEnabled()) {
				logger.debug(info);
			}
		case 1 :
			if (logger.isInfoEnabled()) {
				logger.info(info);
			}
			break;
		case 2 :
			if (logger.isTraceEnabled()) {
				logger.trace(info);
			}
			break;
		case 3 :
			logger.warn(info);
			break;
		case 4 :
			logger.error(info);
			break;
		default:
			break;
		}
	}

	public Cad_Oper() {
		this.m_subTptRectList = new ArrayList<Cad_Rect>();
		this.byqNameList = new LinkedHashMap<String, String>();
		this.zsbyqList = new LinkedHashMap<String, Zsbyq>();
		this.pdbyqList = new LinkedHashMap<String, Pdbyq>();
		
	}
	
	String getDateString(){
		 Calendar ca = Calendar.getInstance();
	      int year = ca.get(Calendar.YEAR);//获取年份
	      int month=ca.get(Calendar.MONTH)+1;//获取月份
	      int day=ca.get(Calendar.DATE);//获取日
	      int minute=ca.get(Calendar.MINUTE);//分
	      int hour=ca.get(Calendar.HOUR);//小时
	      int second=ca.get(Calendar.SECOND);//秒
//	      int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
	      String str = String.format("%d%02d%02d%02d%02d%02d", year,month,day,hour,minute,second);
	      return str;
	}
	public int getByqNameListByID(String byqID) {
		byqNameList.clear();
		zsbyqList.clear();
		pdbyqList.clear();
		Zsbyq zsbyq = null;
		Pdbyq pdbyq = null;

		zsbyq = zsbyqMapper.selectByPrimaryKey(byqID);
		if (zsbyq != null){
			zsbyqList.put(zsbyq.getDeviceid(), zsbyq);
			byqNameList.put(zsbyq.getDeviceid(), zsbyq.getDevicename());
		}
		else{
			pdbyq = pdbyqMapper.selectByPrimaryKey(byqID);
			if (pdbyq != null){
				pdbyqList.put(pdbyq.getDeviceid(), pdbyq);
				byqNameList.put(pdbyq.getDeviceid(), pdbyq.getDevicename());
			}
		}
		return (byqNameList.size());
	}
	public int getByqNameList(long commpanyId) {
		byqNameList.clear();
		zsbyqList.clear();
		pdbyqList.clear();

		List<Zsbyq> zlist = zsbyqMapper.selectDeviceList(null, commpanyId, null, 0, null, null, null, 0, 0);
		List<Pdbyq> plist= pdbyqMapper.selectDeviceList(null, commpanyId, null, 0, null, null, null, 0, 0);

		for (int i=0; i<zlist.size(); i++){
			zsbyqList.put(zlist.get(i).getDeviceid(), zlist.get(i));
			byqNameList.put(zlist.get(i).getDeviceid(), zlist.get(i).getDevicename());
		}

		for (int i=0; i<plist.size(); i++){
			pdbyqList.put(plist.get(i).getDeviceid(), plist.get(i));
			byqNameList.put(plist.get(i).getDeviceid(), plist.get(i).getDevicename());
		}
		return (byqNameList.size());
	}
	

    /**
	 * 生成拓扑图的入口主函数
	 * @param companyId : 公司ID
	 * @param pString byqId ： 变压器ID， 如果为空表示生成所有变压器
	 * @return boolean
	 */
	public String drawTpt(String path,Long companyId, String byqId, String byqName) {
		boolean result = false;
		String strDate = getDateString();
		
		this.companyId = companyId;

		Log(LOG_DEBUG, "drawTpt:companyId=" + companyId + "|byqId=" + byqId + "|byqName=" + byqName);
		
		ArrayList<String> singleByqList = null;
		ArrayList<String> allByqList = new ArrayList<String>();
		Cad_Point point = null;

		if (byqId != null && byqId.length() > 0) {
			getByqNameListByID(byqId);
			ArrayList<String> arrayList = drawByq(byqId, byqName, new Cad_Point(0.0D, 0.0D));
			if (arrayList != null) {
				return writeDrawFile(path,strDate, byqName + "单线图.scr", arrayList);
			}
		} else {

			if (getByqNameList(companyId) > 0) {
				Log(LOG_INFO, "drawTpt: 准备计算所有变压器拓扑数据, 变压器数量："	+ this.byqNameList.size() + "个！");
				point = new Cad_Point(0.0D, 0.0D);
				 for (Iterator<String> iterator = byqNameList.keySet().iterator(); iterator.hasNext();) {   
					 String strID = iterator.next();
					 String strName = byqNameList.get(strID);

					 singleByqList = drawByq(strID, strName, point);
					 if (singleByqList != null) {
						allByqList.addAll(singleByqList);
						writeDrawFile(path,strDate, strName + "拓扑.scr",
								singleByqList);
						Log(LOG_INFO, "drawTpt: 变压器:" + strName + " 拓扑数据已经保存到单独文件!，");
					}
				}

				if (allByqList.size() > 0) {
					writeDrawFile(path,strDate, strDate + this.byqNameList.size()
							+ "个变压器拓扑图合集.scr", allByqList);
					Log(LOG_INFO, "drawTpt: 所有拓扑数据已经保存到合集文件..." + this.byqNameList.size() + "个！");
					result = true;
				} else {
					Log(LOG_INFO, "drawTpt: 没有拓扑数据可生成!");
				}
			} else {
				Log(LOG_INFO, "drawTpt: 没有变压器可生成!");
			}
		}
		if (logger.isDebugEnabled()) {
			Log(LOG_INFO, "drawTpt:companyId=" + companyId + "|byqId=" + byqId+ "|result=" + result);
		}
		return null;
	}

	public ByqTpt getByqTpt(String byqId, String byqName, Cad_Point point) {
		Log(LOG_DEBUG, "getByqTpt:byqId=" + byqId);
		Log(LOG_INFO, "getByqTpt:byqId=" + byqId+" 正在分析变压器拓扑对象...!");

		ArrayList<Cad_Device>  deviceList = getByqAllDrv(byqId, byqName);
		if (deviceList == null || deviceList.size()<=0) {
			Log(LOG_INFO, "getByqTpt:byqId=" + byqId+" 变压器没有子设备!");
			return null;
		}

		ByqTpt byqTpt = new ByqTpt();
		byqTpt.deviceList.addAll(deviceList);
		byqTpt.calcDevLineRect();
		if (this.tmp_byqjwd != null) {
			byqTpt.mapRect.pt1.copy(Cad_BaseInfo.getxy(this.tmp_byqjwd.pt1));
			byqTpt.mapRect.pt2.copy(Cad_BaseInfo.getxy(this.tmp_byqjwd.pt2));
		}

		Log(LOG_INFO, "getByqTpt:byqId=" + byqId +"|byqName="+byqName+ " 正在调整绘图比例...");

		Cad_Point minPoint = Cad_BaseInfo.getxy(tmp_byqjwd.pt1.x, tmp_byqjwd.pt1.y);
        byqTpt.moveof(new Cad_Point(-minPoint.x+40,-minPoint.y+10));
		byqTpt.ratio = autoZBBL(byqTpt.deviceList);
		if (this.tmp_byqjwd != null) {
			Cad_Device unit1 = new Cad_Device();
			unit1.drawRect = byqTpt.mapRect;
			unit1.linkPoint.copy(byqTpt.mapRect.pt1);
			unit1.changeRatio(1.0D, byqTpt.ratio, true);
		}
		byqTpt.calcDevLineRect();
		byqTpt.rect.set(byqTpt.deviceLineRect);

		Log(LOG_INFO, "getByqTpt:byqId=" + byqId+"|byqName="+byqName+" 正在调整绘图位置...");
		
		
//		byqTpt.moveof(new Cad_Point(10.0D + (-byqTpt.rect.left + point.x + Cad_BaseInfo.tl_w), 10.0D + (-byqTpt.rect.bottom + point.y)));
//		Cad_Point minPoint = Cad_BaseInfo.getxy(tmp_byqjwd.pt1.x, tmp_byqjwd.pt1.y);
//		byqTpt.moveof(new Cad_Point(-minPoint.x+40,-minPoint.y+10));
		
		Log(LOG_INFO, "getByqTpt:byqId=" + byqId+"|byqName="+byqName+"正在连接干线拓扑线路...");		

		ArrayList<Cad_Line> lineList = getByqAllDxDl_Lines(byqId, byqName, byqTpt.deviceList);
		if (lineList != null) {
			byqTpt.lineList.addAll(lineList);
		}

		Log(LOG_INFO, "getByqTpt:byqId=" + byqId+"|byqName="+byqName+"正在补全干线拓扑线路...");		
		
		lineList.clear();
		lineList = getAllBl_Lines(byqTpt.deviceList);
		if (lineList != null)
			byqTpt.lineList.addAll(lineList);
		
		lineList.clear();
		lineList = getAllLinesLinkedBX(byqTpt.deviceList);
		if (lineList != null)
			byqTpt.lineList.addAll(lineList);
		
		Cad_Device unit2 = new Cad_Device();
		Object[] arrayOfObject = new Object[1];
		
		arrayOfObject[0] = Double.valueOf(100.0D * byqTpt.ratio);
		unit2.set(byqId, String.format("%1$.0f米", arrayOfObject), "","", "",	true, 
				Cad_BaseInfo.BLOCK_TYPE_TL, true, false, point);
		byqTpt.deviceList.add(0, unit2);
		byqTpt.rect.pt1.copy(point);

		Log(LOG_INFO, "getByqTpt:byqId=" + byqId+"|byqName="+byqName+"已完成表箱拓扑线计算!");		
		Log(LOG_INFO, "getByqTpt:byqId=" + byqId+"|byqName="+byqName+"已完成主线路拓扑线计算!");		
		
		return byqTpt;
	}

	public String writeDrawFile(String path,String strDate, String fileName,
			ArrayList<String> pArrayList) {
		boolean result= false;
		File rootPath = new File(path);
		FileOutputStream os;
		if (pArrayList == null)
			return null;
		if (pArrayList.size() < 1)
			return null;
		
			try {
				if(!rootPath.exists()){
					rootPath.mkdirs();
				}
				File file = new File(path+strDate+fileName);
				os = new FileOutputStream(file);
				if (os != null) {
					writeCadHead(os);
					for (int i = 0;i < pArrayList.size(); i++) {
						os.write(((String) pArrayList.get(i)).getBytes("GBK"));
					}
					os.write(Cad_BaseInfo.CAD_osmode_set.getBytes("GBK"));
					os.write(Cad_BaseInfo.CAD_view_all.getBytes("GBK"));
					os.write("layer s 0 ".getBytes("GBK"));
//					os.write("\r\n".getBytes("GBK"));//本地服务器需加
					os.close();
					return strDate+fileName;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		return null;
//		if (pString1 == null)
//			;
//		FileOutputStream localFileOutputStream;
//		do
//			try {
//				localFileOutputStream = FileOper.new_outSd_File(
//						this.context.getString(Cad_BaseInfo.CAD_cadtpt_dir),
//						pString2);
//				continue;
//				if (pString1.isEmpty()) {
//					localFileOutputStream = FileOper.new_outSd_File(
//							this.context.getString(Cad_BaseInfo.CAD_cadtpt_dir),
//							pString2);
//				} else {
//					localFileOutputStream = FileOper.new_outSd_File(
//							this.context.getString(Cad_BaseInfo.CAD_cadtpt_dir) + "/"
//									+ pString1, pString2);
//					continue;
//					writeCadHead(localFileOutputStream);
//					for (int i = 0;; i++) {
//						if (i >= pArrayList.size()) {
//							localFileOutputStream.write(this.context.getString(
//									Cad_BaseInfo.CAD_osmode_set).getBytes("GBK"));
//							localFileOutputStream.write(this.context.getString(
//									Cad_BaseInfo.CAD_view_all).getBytes("GBK"));
//							localFileOutputStream.write("layer s 0 \r\n"
//									.getBytes("GBK"));
//							localFileOutputStream.close();
//							return true;
//						}
//						localFileOutputStream.write(((String) pArrayList
//								.get(i)).getBytes("GBK"));
//					}
//				}
//			} catch (IOException localIOException) {
//				localIOException.printStackTrace();
//				return false;
//			}
//		while (localFileOutputStream != null);
//		return false;
	}

	public boolean _mdzbbl(ArrayList<Cad_Device> pArrayList, double d1, double ratio) {
		if (pArrayList == null) {
			return false;
		}
		
		for (int i = 0; i<pArrayList.size(); i++) {
			pArrayList.get(i).changeRatio(d1, ratio);
		}
		return true;
	}

	public boolean writeCadHead(FileOutputStream out) {
		boolean result = false;
		
		if ((out == null))
			return result;

		try {
//			FileInputStream in = new FileInputStream("cadhandle.txt");
			FileInputStream in = new FileInputStream(PwtzConstants.WEB_REAL_PATH+"/cad/dhead.txt");
			if (in != null) {
				byte[] arrayOfByte = new byte[1000];
				int i = in.read(arrayOfByte);
				while(i>0) {
					out.write(arrayOfByte, 0, i);
					i = in.read(arrayOfByte);
				}
				in.close();
				result = true;
			}
		} catch (IOException localIOException) {
			localIOException.printStackTrace();
		}
		return result;
	}

	public double autoZBBL(ArrayList<Cad_Device>  deviceList) {
		if (deviceList == null) {
			return -1.0D;
		}
		
		double d1 = Math.floor(100.0D * (drvs_minjl(deviceList) / Cad_BaseInfo.min_jl));
		if (d1 < 100.0D) {
			d1 = 100.0D;
		}
		
		double ratio = d1 / 100.0D;
		_mdzbbl(deviceList, 1.0D, ratio);
		
		return ratio;
	}

	public ArrayList<String> drawByq(String byqId, String byqName, Cad_Point point) {
		ByqTpt byqTpt = getByqTpt(byqId, byqName, point);
		if (byqTpt == null)
			return null;

		ArrayList<String>  arrayList = new ArrayList<String>();

		Log(LOG_INFO, "drawByq:byqId="+byqId+"|byqName="+byqName+"|正在生成绘图文件图层数据...");
		
		String buffer = new String();
		String temp = "";

		for (int i = 0; i < Cad_BaseInfo.lay_names.length; i++) {
			Log(LOG_INFO, "drawByq:byqId="+byqId+"|byqName="+byqName+"|正在生成 [" + Cad_BaseInfo.lay_names[i] + "] 层文件数据");

			
			Object[] objs = new Object[1];
			objs[0] = Cad_BaseInfo.lay_names[i];
			
			//setcur_layer = layer s %1$s
			buffer = buffer	+ String.format(Cad_BaseInfo.CAD_setcur_layer, objs);
			
			for (int j=0; j<byqTpt.deviceList.size(); j++){
				if (byqTpt.deviceList.get(j).layerName.equals(Cad_BaseInfo.lay_names[i])){
					buffer = buffer	+ byqTpt.deviceList.get(j).Draw(false);
				}
			}
			for (int k=0; k<byqTpt.lineList.size(); k++){
				if (byqTpt.lineList.get(k).layerName.equals(Cad_BaseInfo.lay_names[i])){
					buffer = buffer	+ byqTpt.lineList.get(k).Draw(false);
				}
			}
			if (buffer.length() > 7000) {
				arrayList.add(buffer);
				buffer = new String();
			}
		}

		Log(LOG_INFO, "drawByq:byqId="+byqId+"|byqName="+byqName+"|完成所有图层数据生成!");

		byqTpt.calcDevLineRect();
		
		// draw_rectang = rectang %1$f,%2$f %3$f,%4$f
		Object[] arrayOfObject2 = new Object[4];
		arrayOfObject2[0] = Double.valueOf(byqTpt.rect.pt1.x);
		arrayOfObject2[1] = Double.valueOf(byqTpt.rect.pt1.y);
		arrayOfObject2[2] = Double.valueOf(10.0D + byqTpt.deviceLineRect.pt2.x);
		arrayOfObject2[3] = Double.valueOf(10.0D + byqTpt.deviceLineRect.pt2.y);
		
		temp = String.format(Cad_BaseInfo.CAD_draw_rectang, arrayOfObject2);
		buffer = buffer + temp;
		
		// draw_line = line %1$f,%2$f %3$f,%4$f
		Object[] arrayOfObject3 = new Object[4];
		arrayOfObject3[0] = Double.valueOf(byqTpt.mapRect.pt1.x - 3.0D);
		arrayOfObject3[1] = Double.valueOf(byqTpt.mapRect.pt1.y);
		arrayOfObject3[2] = Double.valueOf(3.0D + byqTpt.mapRect.pt1.x);
		arrayOfObject3[3] = Double.valueOf(byqTpt.mapRect.pt1.y);
		temp = String.format(Cad_BaseInfo.CAD_draw_line, arrayOfObject3);
		buffer = buffer + temp;
		
		// draw_line = line %1$f,%2$f %3$f,%4$f
		Object[] arrayOfObject4 = new Object[4];
		arrayOfObject4[0] = Double.valueOf(byqTpt.mapRect.pt1.x);
		arrayOfObject4[1] = Double.valueOf(byqTpt.mapRect.pt1.y - 3.0D);
		arrayOfObject4[2] = Double.valueOf(byqTpt.mapRect.pt1.x);
		arrayOfObject4[3] = Double.valueOf(3.0D + byqTpt.mapRect.pt1.y);
		temp = String.format(Cad_BaseInfo.CAD_draw_line, arrayOfObject4);
		buffer = buffer + temp;
		
		// draw_text = text %1$f,%2$f %3$f 0 %4$s
		Object[] arrayOfObject5 = new Object[4];
		arrayOfObject5[0] = Double.valueOf(3.0D + byqTpt.mapRect.pt1.x);
		arrayOfObject5[1] = Double.valueOf(byqTpt.mapRect.pt1.y - 3.0D);
		arrayOfObject5[2] = Double.valueOf(1.2D);
		arrayOfObject5[3] = ("地图参照坐标: 经度 " + this.tmp_byqjwd.pt1.x + " 纬度 " + this.tmp_byqjwd.pt1.y);
		temp = String.format(Cad_BaseInfo.CAD_draw_text, arrayOfObject5);
		buffer = buffer + temp;
		
		// draw_line = line %1$f,%2$f %3$f,%4$f
		String str7 = Cad_BaseInfo.CAD_draw_line;
		Object[] arrayOfObject6 = new Object[4];
		arrayOfObject6[0] = Double.valueOf(byqTpt.mapRect.pt2.x - 3.0D);
		arrayOfObject6[1] = Double.valueOf(byqTpt.mapRect.pt2.y);
		arrayOfObject6[2] = Double.valueOf(3.0D + byqTpt.mapRect.pt2.x);
		arrayOfObject6[3] = Double.valueOf(byqTpt.mapRect.pt2.y);
		temp = String.format(Cad_BaseInfo.CAD_draw_line, arrayOfObject6);
		buffer = buffer + temp;
		
		// draw_line = line %1$f,%2$f %3$f,%4$f
		Object[] arrayOfObject7 = new Object[4];
		arrayOfObject7[0] = Double.valueOf(byqTpt.mapRect.pt2.x);
		arrayOfObject7[1] = Double.valueOf(byqTpt.mapRect.pt2.y - 3.0D);
		arrayOfObject7[2] = Double.valueOf(byqTpt.mapRect.pt2.x);
		arrayOfObject7[3] = Double.valueOf(3.0D + byqTpt.mapRect.pt2.y);
		temp = String.format(Cad_BaseInfo.CAD_draw_line, arrayOfObject7);
		buffer = buffer + temp;
		
		// draw_text = text %1$f,%2$f %3$f 0 %4$s
		Object[] arrayOfObject8 = new Object[4];
		arrayOfObject8[0] = Double.valueOf(3.0D + byqTpt.mapRect.pt2.x);
		arrayOfObject8[1] = Double.valueOf(byqTpt.mapRect.pt2.y - 3.0D);
		arrayOfObject8[2] = Double.valueOf(1.2D);
		arrayOfObject8[3] = ("地图参照坐标: 经度 " + this.tmp_byqjwd.pt2.x + " 纬度 " + this.tmp_byqjwd.pt2.y);
		temp = String.format(Cad_BaseInfo.CAD_draw_text, arrayOfObject8);
		buffer = buffer + temp;
		
		arrayList.add(buffer);
		point.y += 30.0D + (byqTpt.deviceLineRect.pt2.y - byqTpt.rect.pt1.y);
		byqTpt.deviceList.clear();
		byqTpt.lineList.clear();
		return arrayList;
		// String str1 = new String();
		// ByqTpt byqTpt = getByqTpt(byqId, point,
		//				pArrayList);
//		if ((byqTpt == null) || (this.context == null)
//				|| (this.sqlbas == null))
//			return null;
//		ArrayList arrayList = new ArrayList();
//		Oper_info.startoi(pArrayList, 1, Cad_BaseInfo.lay_names.length,
//				"正在生成绘图文件图层数据");
//		int i = 0;
//
//		if (i >= Cad_BaseInfo.lay_names.length) {
//			Oper_info.finish(pArrayList, 1, "完成所有图层数据生成!");
//			byqTpt.calcDevLineRect();
//			StringBuilder localStringBuilder2 = new StringBuilder(
//					String.valueOf(str1));
//			// draw_rectang = rectang %1$f,%2$f %3$f,%4$f
//			String str3 = this.context.getString(Cad_BaseInfo.CAD_draw_rectang);
//			Object[] arrayOfObject2 = new Object[4];
//			arrayOfObject2[0] = Double.valueOf(byqTpt.rect.pt1.x);
//			arrayOfObject2[1] = Double.valueOf(byqTpt.rect.pt1.y);
//			arrayOfObject2[2] = Double
//					.valueOf(10.0D + byqTpt.deviceLineRect.pt2.x);
//			arrayOfObject2[3] = Double
//					.valueOf(10.0D + byqTpt.deviceLineRect.pt2.y);
//			StringBuilder localStringBuilder3 = new StringBuilder(
//					String.valueOf(String.format(str3, arrayOfObject2)));
//			// draw_line = line %1$f,%2$f %3$f,%4$f
//			String str4 = this.context.getString(Cad_BaseInfo.CAD_draw_line);
//			Object[] arrayOfObject3 = new Object[4];
//			arrayOfObject3[0] = Double
//					.valueOf(byqTpt.mapRect.pt1.x - 3.0D);
//			arrayOfObject3[1] = Double.valueOf(byqTpt.mapRect.pt1.y);
//			arrayOfObject3[2] = Double
//					.valueOf(3.0D + byqTpt.mapRect.pt1.x);
//			arrayOfObject3[3] = Double.valueOf(byqTpt.mapRect.pt1.y);
//			StringBuilder localStringBuilder4 = new StringBuilder(
//					String.valueOf(String.format(str4, arrayOfObject3)));
//			// draw_line = line %1$f,%2$f %3$f,%4$f
//			String str5 = this.context.getString(Cad_BaseInfo.CAD_draw_line);
//
//			Object[] arrayOfObject4 = new Object[4];
//			arrayOfObject4[0] = Double.valueOf(byqTpt.mapRect.pt1.x);
//			arrayOfObject4[1] = Double
//					.valueOf(byqTpt.mapRect.pt1.y - 3.0D);
//			arrayOfObject4[2] = Double.valueOf(byqTpt.mapRect.pt1.x);
//			arrayOfObject4[3] = Double
//					.valueOf(3.0D + byqTpt.mapRect.pt1.y);
//			StringBuilder localStringBuilder5 = new StringBuilder(
//					String.valueOf(String.format(str5, arrayOfObject4)));
//			// draw_text_jd = text %1$f,%2$f %3$f %4$f,%5$f %6$s
//			String str6 = this.context.getString(Cad_BaseInfo.CAD_draw_text);
//
//			Object[] arrayOfObject5 = new Object[4];
//			arrayOfObject5[0] = Double
//					.valueOf(3.0D + byqTpt.mapRect.pt1.x);
//			arrayOfObject5[1] = Double
//					.valueOf(byqTpt.mapRect.pt1.y - 3.0D);
//			arrayOfObject5[2] = Double.valueOf(1.2D);
//			arrayOfObject5[3] = ("地图参照坐标: 经度 " + this.tmp_byqjwd.pt1.x + " 纬度 " + this.tmp_byqjwd.pt1.y);
//			StringBuilder localStringBuilder6 = new StringBuilder(
//					String.valueOf(String.format(str6, arrayOfObject5)));
//			// draw_line = line %1$f,%2$f %3$f,%4$f
//			String str7 = this.context.getString(Cad_BaseInfo.CAD_draw_line);
//			Object[] arrayOfObject6 = new Object[4];
//			arrayOfObject6[0] = Double
//					.valueOf(byqTpt.mapRect.pt2.x - 3.0D);
//			arrayOfObject6[1] = Double.valueOf(byqTpt.mapRect.pt2.y);
//			arrayOfObject6[2] = Double
//					.valueOf(3.0D + byqTpt.mapRect.pt2.x);
//			arrayOfObject6[3] = Double.valueOf(byqTpt.mapRect.pt2.y);
//			StringBuilder localStringBuilder7 = new StringBuilder(
//					String.valueOf(String.format(str7, arrayOfObject6)));
//			// draw_line = line %1$f,%2$f %3$f,%4$f
//			String str8 = this.context.getString(Cad_BaseInfo.CAD_draw_line);
//			Object[] arrayOfObject7 = new Object[4];
//			arrayOfObject7[0] = Double.valueOf(byqTpt.mapRect.pt2.x);
//			arrayOfObject7[1] = Double
//					.valueOf(byqTpt.mapRect.pt2.y - 3.0D);
//			arrayOfObject7[2] = Double.valueOf(byqTpt.mapRect.pt2.x);
//			arrayOfObject7[3] = Double
//					.valueOf(3.0D + byqTpt.mapRect.pt2.y);
//			StringBuilder localStringBuilder8 = new StringBuilder(
//					String.valueOf(String.format(str8, arrayOfObject7)));
//			// draw_text_jd = text %1$f,%2$f %3$f %4$f,%5$f %6$s
//			String str9 = this.context.getString(Cad_BaseInfo.CAD_draw_text);
//			Object[] arrayOfObject8 = new Object[4];
//			arrayOfObject8[0] = Double
//					.valueOf(3.0D + byqTpt.mapRect.pt2.x);
//			arrayOfObject8[1] = Double
//					.valueOf(byqTpt.mapRect.pt2.y - 3.0D);
//			arrayOfObject8[2] = Double.valueOf(1.2D);
//			arrayOfObject8[3] = ("地图参照坐标: 经度 " + this.tmp_byqjwd.pt2.x + " 纬度 " + this.tmp_byqjwd.pt2.y);
//			arrayList.add(String.format(str9, arrayOfObject8));
//			point.y += 30.0D + (byqTpt.deviceLineRect.pt2.y - byqTpt.rect.pt1.y);
//			byqTpt.byqDeviceList.clear();
//			byqTpt.lineList.clear();
//			return arrayList;
//		}
//		Oper_info.next(pArrayList, 1, "正在生成 [" + Cad_BaseInfo.lay_names[i]
//				+ "] 层文件数据");
//		if (!Oper_info.can_run(pArrayList, 1))
//			return null;
//		StringBuilder localStringBuilder1 = new StringBuilder(
//				String.valueOf(str1));
//
//		// setcur_layer = layer s %1$s
//		String str2 = this.context.getString(Cad_BaseInfo.CAD_setcur_layer);
//		Object[] arrayOfObject1 = new Object[1];
//		arrayOfObject1[0] = Cad_BaseInfo.lay_names[i];
//		str1 = String.format(str2, arrayOfObject1);
//		int j = 0;
//		label1175: if (j >= byqTpt.byqDeviceList.size())
//			if (i < -2 + Cad_BaseInfo.lay_names.length)
//				;
//		for (int k = 0;; k++) {
//			if (k >= byqTpt.lineList.size()) {
//				if (str1.length() > 7000) {
//					arrayList.add(str1);
//					str1 = new String();
//				}
//				i++;
//				break;
//				if (((Cad_Device ) byqTpt.byqDeviceList.get(j)).layerName
//						.equals(Cad_BaseInfo.lay_names[i]))
//					str1 = str1
//							+ ((Cad_Device ) byqTpt.byqDeviceList.get(j)).Draw(
//									this.context, false);
//				j++;
//				break label1175;
//			}
//			if (((Cad_Line) byqTpt.lineList.get(k)).layerName
//					.equals(Cad_BaseInfo.lay_names[i]))
//				str1 = str1
//						+ ((Cad_Line) byqTpt.lineList.get(k)).Draw(
//								this.context, false);
//		}
	}

	public int drv_wztz(ArrayList<Cad_Device> pArrayList) {
		int i = 0;
		
		for (int j=0; j<pArrayList.size(); j++)
		{
			Cad_Device unit1 = (Cad_Device) pArrayList.get(j);
			if (!unit1.isShow) {
				continue;
			}
			for (int k = j + 1; k < pArrayList.size(); k++) {
				Cad_Device unit2 = (Cad_Device) pArrayList.get(k);
				if (unit2.isShow) {
					Cad_Rect localEx_drect = Cad_Rect._getsamerc(
							unit1.drawRect, unit2.drawRect);
					if (localEx_drect != null) {
						unit2.moveof(localEx_drect.pt1);
						i ++;
					}
				}
			}
			
		}
		return i;
		
//		int i = 0;
//		int j = 0;
//		if (j >= pArrayList.size())
//			return i;
//		Cad_Device  unit1 = (Cad_Device ) pArrayList.get(j);
//		if (!unit1.isShow)
//			;
//		while (true) {
//			j++;
//			break;
//			for (int k = j + 1; k < pArrayList.size(); k++) {
//				Cad_Device  unit2 = (Cad_Device ) pArrayList.get(k);
//				if (unit2.isShow) {
//					Ex_drect localEx_drect = Ex_drect._getsamerc(
//							unit1.drawRect, unit2.drawRect);
//					if (localEx_drect != null) {
//						unit2.moveof(localEx_drect.pt1);
//						i++;
//					}
//				}
//			}
//		}
	}

	public double drvs_minjl(ArrayList<Cad_Device> deviceList) {
		double d = 100000.0D;
		for (int i=0; i<deviceList.size(); i++) {
			Cad_Device dev1 =  deviceList.get(i);
			if (!dev1.isShow) {
				continue;
			}
			for (int j = i + 1; j < deviceList.size(); j++) {
				Cad_Device dev2 = deviceList.get(j);
				if (dev2.isShow){
					d = Math.min(d, Cad_Line._getjl(dev1.linkPoint,	dev2.linkPoint));
				}
			}
		}
		return d;
	}

	public ArrayList<Cad_Line> getAllBl_Lines(ArrayList<Cad_Device> deviceList) {
		
		if (deviceList == null)
			return null;
		
		ArrayList<Cad_Line>  lineList = new ArrayList<Cad_Line> ();

		for (int i = 0; i < deviceList.size(); i++) {
			Cad_Device dev = deviceList.get(i);
			if ((!dev.linked) && 
					(dev.blockType != Cad_BaseInfo.BLOCK_TYPE_DYDLJT) &&  //2
					(dev.blockType != Cad_BaseInfo.BLOCK_TYPE_DYBX)) { //9
				Cad_Device unit2 = new Cad_Device();
				unit2.deviceName = dev.parentDeviceName;
				int j = deviceList.indexOf(unit2);
				if (j <= -1) {
					if ((dev.blockType != Cad_BaseInfo.BLOCK_TYPE_ZSBYQ) && 
							(dev.blockType != Cad_BaseInfo.BLOCK_TYPE_PDBYQ)) {
						Cad_Device unit3 = getNearUnit(deviceList,	dev);
						if (unit3 != null) {
							Cad_Point point1 = dev.getlinkpt(unit3.linkPoint);
							Cad_Point point2 = unit3.getlinkpt(dev.linkPoint);
							if ((point1 != null)
									&& (point2 != null)) {
								String str1 = Cad_Device.getMinSubName(((Cad_Device) deviceList.get(0)).deviceName,	dev.lineName);
								String str2 = "导线";
								if (((dev.blockType != Cad_BaseInfo.BLOCK_TYPE_DYGT_ZXGT) && 
										(dev.blockType != Cad_BaseInfo.BLOCK_TYPE_DYGT_NZGT)) || 
										((unit3.blockType != Cad_BaseInfo.BLOCK_TYPE_DYGT_ZXGT) && 
												(unit3.blockType != Cad_BaseInfo.BLOCK_TYPE_DYGT_NZGT)	&& 
												(unit3.blockType != Cad_BaseInfo.BLOCK_TYPE_ZSBYQ) && 
												(dev.blockType != Cad_BaseInfo.BLOCK_TYPE_ZSBYQ))) {
									str2 = "电缆";
								}
								Cad_Line line1 = new Cad_Line();
								line1.set(str1, "", "", str2,
										dev.lineName, 0, new Cad_Rect(point1, point2));
								lineList.add(line1);
							}
							dev.linked = true;
						}
					}
				} else {
					Cad_Device unit4 = (Cad_Device) deviceList.get(j);
					Cad_Point point3 = dev.getlinkpt(unit4.linkPoint);
					Cad_Point point4 = unit4.getlinkpt(dev.linkPoint);
					
					if ((point3 != null) && (point4 != null)) {
						String str3 = Cad_Device.getMinSubName(deviceList.get(0).deviceName, dev.lineName);
						String str4 = "导线";
						if (((dev.blockType != Cad_BaseInfo.BLOCK_TYPE_DYGT_ZXGT) && 
								(dev.blockType != Cad_BaseInfo.BLOCK_TYPE_DYGT_NZGT))  || 
								((unit4.blockType != Cad_BaseInfo.BLOCK_TYPE_DYGT_ZXGT) && 
										(unit4.blockType != Cad_BaseInfo.BLOCK_TYPE_DYGT_NZGT) && 
										(unit4.blockType != Cad_BaseInfo.BLOCK_TYPE_ZSBYQ) && 
										(dev.blockType != Cad_BaseInfo.BLOCK_TYPE_ZSBYQ))) {
							str4 = "电缆";
						}
						Cad_Line line2 = new Cad_Line();
						line2.set(str3, "", "", str4,
								dev.lineName, 0, new Cad_Rect(point3, point4));
						lineList.add(line2);
					}
					dev.linked = true;
				}
			}
		}

		if (lineList.size() <0) {
			return null;
		}
		
		return lineList;
		

//		if (pArrayList == null)
//			localObject = null;
//		int i;
//		do {
//			return localObject;
//			localObject = new ArrayList();
//			i = 0;
//			if (i < pArrayList.size())
//				break;
//		} while (((ArrayList) localObject).size() > 0);
//		return null;
//		Cad_Device  unit1 = (Cad_Device ) pArrayList.get(i);
//		if ((!unit1.linked) && (unit1.blockType != 2)
//				&& (unit1.blockType != 9)) {
//			Cad_Device  unit2 = new Cad_Device ();
//			unit2.deviceName = unit1.parentDeviceName;
//			int j = pArrayList.indexOf(unit2);
//			if (j <= -1)
//				break label291;
//			Cad_Device  unit4 = (Cad_Device ) pArrayList.get(j);
//			Ex_dpoint point3 = unit1
//					.getlinkpt(unit4.linkPoint);
//			Ex_dpoint point4 = unit4
//					.getlinkpt(unit1.linkPoint);
//			if ((point3 != null) && (point4 != null)) {
//				String str3 = Cad_Device .getMinSubName(
//						((Cad_Device ) pArrayList.get(0)).deviceName,
//						unit1.lineName);
//				String str4 = "导线";
//				if (((unit1.blockType != 0) && (unit1.blockType != 1))
//						|| ((unit4.blockType != 0)
//								&& (unit4.blockType != 1)
//								&& (unit4.blockType != 3) && (unit1.blockType != 3)))
//					str4 = "电缆";
//				Cad_Line line2 = new Cad_Line();
//				line2.set(str3, "", "", str4, unit1.lineName, 0,
//						new Ex_drect(point3, point4));
//				((ArrayList) localObject).add(line2);
//			}
//			unit1.linked = true;
//		}
//		while (true) {
//			i++;
//			break;
//			label291: if ((unit1.blockType != 3)
//					&& (unit1.blockType != 4)) {
//				Cad_Device  unit3 = getNearUnit(pArrayList,
//						unit1);
//				if (unit3 != null) {
//					Ex_dpoint point1 = unit1
//							.getlinkpt(unit3.linkPoint);
//					Ex_dpoint point2 = unit3
//							.getlinkpt(unit1.linkPoint);
//					if ((point1 != null) && (point2 != null)) {
//						String str1 = Cad_Device .getMinSubName(
//								((Cad_Device ) pArrayList.get(0)).deviceName,
//								unit1.lineName);
//						String str2 = "导线";
//						if (((unit1.blockType != 0) && (unit1.blockType != 1))
//								|| ((unit3.blockType != 0)
//										&& (unit3.blockType != 1)
//										&& (unit3.blockType != 3) && (unit1.blockType != 3)))
//							str2 = "电缆";
//						Cad_Line line1 = new Cad_Line();
//						line1.set(str1, "", "", str2,
//								unit1.lineName, 0, new Ex_drect(
//										point1, point2));
//						((ArrayList) localObject).add(line1);
//					}
//					unit1.linked = true;
//				}
//			}
//		}
	}

	public ArrayList<Cad_Line> getAllLinesLinkedBX(ArrayList<Cad_Device> deviceList) {
		if (deviceList == null) {
			return null;
		}
		ArrayList<Cad_Line>  resultLineList = new ArrayList<Cad_Line> ();
		
		//int i = 0;
		
		Log(LOG_INFO,"getAllLinesLinkedBX:" + "准备生成表箱拓扑数据");
		
		for (int k=0; k<deviceList.size(); k++){
			Cad_Device cadUnit = deviceList.get(k);
			if (cadUnit.hasBX) {
				Log(LOG_INFO,"getAllLinesLinkedBX:" +"正在生成 " + cadUnit.showName + " 的表箱拓扑线");

				ArrayList<Cad_Line> list = getSlaveBXLinkedLines(deviceList, cadUnit);
				if (list != null) {
					resultLineList.addAll(list);
				}
			}
		}
		if ((resultLineList).size() < 0)
			return null;
		
		return resultLineList;
		
//		if (pArrayList == null) {
//			localObject = null;
//			return localObject;
//		}
//		Object localObject = new ArrayList();
//		int i = 0;
//		int j = 0;
//		label22: if (j >= pArrayList.size())
//			Oper_info.startoi(pArrayList1, 1, i, "准备生成表箱拓扑数据");
//		for (int k = 0;; k++) {
//			if (k >= pArrayList.size()) {
//				if (((ArrayList) localObject).size() > 0)
//					break;
//				return null;
//				i++;
//				j++;
//				break label22;
//			}
//			if (!Oper_info.can_run(pArrayList1, 1))
//				return null;
//			Cad_Device  cadUnit = (Cad_Device ) pArrayList.get(k);
//			if (cadUnit.hasBX) {
//				Oper_info.next(pArrayList1, 1, "正在生成 "
//						+ cadUnit.showName + " 的表箱拓扑线");
//				Oper_info localOper_info = null;
//				if (pArrayList1 != null)
//					localOper_info = (Oper_info) pArrayList1.get(2);
//				ArrayList localArrayList = getSlaveBXLinkedLines(pArrayList,
//						cadUnit, localOper_info);
//				if (localArrayList != null)
//					((ArrayList) localObject).addAll(localArrayList);
//			}
//		}
	}

	public ArrayList<Cad_Line> getAllDxdl_Lines(ArrayList<DXDL> dxdlList,
			ArrayList<Cad_Device> deviceList, boolean pBoolean) {
		if ((dxdlList == null) || (deviceList == null)) {
			return null;
		}
		ArrayList<Cad_Line> resultList = new ArrayList<Cad_Line>();
		
		for (int i = 0; i <dxdlList.size(); i++) {
			ArrayList<Cad_Line>  list = getDxdl_Lines( dxdlList.get(i), deviceList,	pBoolean);
			if (list != null) {
				resultList.addAll(list);
			}
		}
		return resultList;
	}

	public ArrayList<Cad_Device> getAllSubDevice(String deviceID, String deviceName) {
		//根据关联设备查找杆塔 2015/1/17
		ArrayList<Cad_Device> listDygt = getDYGT(deviceID, deviceName);
		////根据关联设备查找站房、表箱、低压剩余电流保护器 2015/1/17
		ArrayList<Cad_Device> listSubDev = getOtsubs(deviceID, deviceName);
		
		if ((listDygt == null) && (listSubDev == null))
			return null;
		
		if (listDygt != null){
			if (listSubDev != null && listSubDev.size()>0) {
				listDygt.addAll(listSubDev);
			}
			return listDygt;
		}else{
			return listSubDev;
		}
	}

	public ArrayList<Cad_Line> getBxLinebyRect(ArrayList<Cad_Rect> rectList) {
		if (rectList == null) {
			return null;
		}
		
		ArrayList<Cad_Line> resultList = new ArrayList<Cad_Line>();
		
		for (int i=0; i<rectList.size(); i++){
			Cad_Rect rect = rectList.get(i);
			Cad_Line cadLine = new Cad_Line();
			cadLine.set("", "", "", "电缆", "", Cad_Line.line_dl, rect);
			resultList.add(cadLine);
		}
		return resultList;

//		Object localObject = new ArrayList();
//		for (int i = 0;; i++) {
//			if (i >= pArrayList.size()) {
//				if (((ArrayList) localObject).size() > 0)
//					break;
//				return null;
//			}
//			Ex_drect localEx_drect = (Ex_drect) pArrayList.get(i);
//			Cad_Line cadLine = new Cad_Line();
//			cadLine.set("", "", "", "电缆", "", 2, localEx_drect);
//			((ArrayList) localObject).add(cadLine);
//		}
	}

	public ArrayList<Cad_Line> getDxdl_Lines(DXDL dxdl,
			ArrayList<Cad_Device> deviceList, boolean pBoolean) {
		if ((dxdl == null) || (deviceList == null)) {
			return null;
		}
		int j =0;
		
		ArrayList<Cad_Line>  resultList = new ArrayList<Cad_Line>();
		Cad_Device unit1 = new Cad_Device();
//		unit1.deviceName = dxdl.enddrv;
		unit1.deviceID = dxdl.enddrv;
		int i = deviceList.indexOf(unit1);
		if (i <= -1)
			return null;
		Cad_Device dev = (Cad_Device) deviceList.get(i);
		Cad_Point point = dev.linkPoint;

		unit1.deviceName = dev.parentDeviceName;
		int k = deviceList.indexOf(unit1);
		if (k <= -1) {
			j = 1;
		} else {
			Cad_Device unit2 = (Cad_Device) deviceList.get(k);
			Cad_Point point1 = unit2.linkPoint;
			Cad_Point point2 = dev.getlinkpt(point1);
			Cad_Point point3 = unit2.getlinkpt(point);
			Cad_Line cadLine = new Cad_Line();
			if ((point2 != null) && (point3 != null)) {
				String str1 = Cad_Device.getMinSubName(dxdl.byqname,
						dxdl.name);
				String str2 = "导线";
				if (!pBoolean)
					str2 = "电缆";
				cadLine.set(dxdl.name, str1, "", str2,
						dxdl.linecode, 0, new Cad_Rect(point2,
								point3));
				resultList.add(cadLine);
			}
			dev.linked = true;
			if ((unit2.deviceName.equals(dxdl.startdrv))
					|| (unit2.linked)) {
				unit1.deviceName = unit2.parentDeviceName;
				if (deviceList.indexOf(unit1) == -1)
					unit2.linked = true;
				j = 1;
			}
			dev = unit2;
			point = point1;
		}
		if (j == 1 && resultList.size() <= 0 ) {
			return  null;
		}
			
		return  resultList;
//		int j = 0;
//		while (true) {
//			if (j != 0) {
//				if (((ArrayList) localObject1).size() > 0)
//					break;
//				return null;
//			}
//			unit1.deviceName = ((Cad_Device ) localObject2).parentDeviceName;
//			int k = pArrayList.indexOf(unit1);
//			if (k <= -1) {
//				j = 1;
//			} else {
//				Cad_Device  unit2 = (Cad_Device ) pArrayList.get(k);
//				Ex_dpoint point1 = unit2.linkPoint;
//				Ex_dpoint point2 = ((Cad_Device ) localObject2)
//						.getlinkpt(point1);
//				Ex_dpoint point3 = unit2
//						.getlinkpt((Ex_dpoint) localObject3);
//				Cad_Line cadLine = new Cad_Line();
//				if ((point2 != null) && (point3 != null)) {
//					String str1 = Cad_Device .getMinSubName(p_dxdl.byq,
//							p_dxdl.name);
//					String str2 = "导线";
//					if (!pBoolean)
//						str2 = "电缆";
//					cadLine.set(p_dxdl.name, str1, "", str2,
//							p_dxdl.xl, 0, new Ex_drect(point2,
//									point3));
//					((ArrayList) localObject1).add(cadLine);
//				}
//				((Cad_Device ) localObject2).linked = true;
//				if ((unit2.deviceName.equals(p_dxdl.startdrv))
//						|| (unit2.linked)) {
//					unit1.deviceName = unit2.parentDeviceName;
//					if (pArrayList.indexOf(unit1) == -1)
//						unit2.linked = true;
//					j = 1;
//				}
//				localObject2 = unit2;
//				localObject3 = point1;
//			}
//		}
	}
	
	//根据关联设备查找 2015/1/17
	public ArrayList<Cad_Device> getOtsubs(String preDeviceID, String preDeviceName) {
		ArrayList<Cad_Device> resultList = new ArrayList<Cad_Device>();
//		public static final String[] otlay = { "表箱", "剩余电流保护器", "配电箱", "分接箱" };
//		public static final int[][] otsub = { { 9, 9 }, { 11, 6 }, { 15, 7 },{ 16, 8 } };
		int deviceType = 0;
		if(zsbyqList.containsKey(preDeviceID)){
			deviceType = 2;//柱上变压器
		} else if(pdbyqList.containsKey(preDeviceID)){
			deviceType =  1;//配电变压器
		}
		List<Dyxl> dyxlList = dyxlMapper.selectByPdbyq(deviceType, preDeviceID, null, companyId, 0, 0);
		if(dyxlList!=null && !dyxlList.isEmpty()){
			for(int j=0; j<dyxlList.size();j++){
				String lineCode = dyxlList.get(j).getDeviceid();
				//1"表箱"
				List<Dybx> listDybx = dybxMapper.selectDeviceList(null, null, null, 0, null, lineCode, null, null, 0, 0);
				if (listDybx!=null && listDybx.size()>0) {
					for (int i=0; i<listDybx.size(); i++) {
			          int blockType = Cad_BaseInfo.BLOCK_TYPE_DYBX; //表箱 BLOCK_TYPE_DYBX = 9; // 低压表箱
			          String deviceID = listDybx.get(i).getDeviceid(); 
			          String devicename = listDybx.get(i).getDevicename(); //设备名称
			          String longitude = listDybx.get(i).getLongitude();  //经度
			          String latitude = listDybx.get(i).getLatitude(); //纬度,所属低压线路名称
			          String linename = listDybx.get(i).getLinecode();  //所属低压线路名称--
			          Cad_Point point = Cad_BaseInfo.getxy(longitude, latitude);
			          tmpgps_save(Double.valueOf(longitude).doubleValue(), Double.valueOf(latitude).doubleValue());
			          Cad_Device cadUnit = new Cad_Device();
			          cadUnit.set(deviceID, devicename, preDeviceID, preDeviceName, linename, true, blockType, true, true, point);
			          cadUnit.lineName = linename;
			          resultList.add(cadUnit);
					}
				}
				//2  "剩余电流保护器"
				List<Dysydlbhq> listDysydlbhq = dysydlbhqMapper.selectDeviceList(null, companyId, null, 0, null, lineCode, null, null, 0, 0);
				if (listDysydlbhq!=null && listDysydlbhq.size()>0) {
					for (int i=0; i<listDysydlbhq.size(); i++) {
			          int blockType = Cad_BaseInfo.BLOCK_TYPE_DYSYDLBHQ; //BLOCK_TYPE_DYSYDLBHQ = 6;// 低压剩余电流保护器
			          String deviceID = listDysydlbhq.get(i).getDeviceid(); 
			          String devicename = listDysydlbhq.get(i).getDevicename(); //设备名称
			          String longitude = listDysydlbhq.get(i).getLongitude();  //经度
			          String latitude = listDysydlbhq.get(i).getLatitude(); //纬度,所属低压线路名称
			          String linename = listDysydlbhq.get(i).getLinecode();  //所属低压线路名称--
			          Cad_Point point = Cad_BaseInfo.getxy(longitude, latitude);
			          tmpgps_save(Double.valueOf(longitude).doubleValue(), Double.valueOf(latitude).doubleValue());
			          Cad_Device cadUnit = new Cad_Device();
			          cadUnit.set(deviceID, devicename, preDeviceID, preDeviceName, linename, true, blockType, true, true, point);
			          cadUnit.lineName = linename;
			          cadUnit.hasBX = hasBX(deviceID);
			          resultList.add(cadUnit);
					}
				}
				//3  "配电箱"
				List<Dypdx> listDypdx = dypdxMapper.selectDeviceList(null, companyId, null, 0, null, lineCode, null, null, 0, 0,null);
				if (listDypdx!=null && listDypdx.size()>0) {
					for (int i=0; i<listDypdx.size(); i++) {
			          int blockType = Cad_BaseInfo.BLOCK_TYPE_DYPDX; //BLOCK_TYPE_DYPDX = 7; // 低压配电箱
			          String deviceID = listDypdx.get(i).getDeviceid(); 
			          String devicename = listDypdx.get(i).getDevicename(); //设备名称
			          String longitude = listDypdx.get(i).getLongitude();  //经度
			          String latitude = listDypdx.get(i).getLatitude(); //纬度,所属低压线路名称
			          String linename = listDypdx.get(i).getLinecode();  //所属低压线路名称--
			          Cad_Point point = Cad_BaseInfo.getxy(longitude, latitude);
			          tmpgps_save(Double.valueOf(longitude).doubleValue(), Double.valueOf(latitude).doubleValue());
			          Cad_Device cadUnit = new Cad_Device();
			          cadUnit.set(deviceID, devicename, preDeviceID, preDeviceName, linename, true, blockType, true, true, point);
			          cadUnit.lineName = linename;
			          cadUnit.hasBX = hasBX(deviceID);
			          resultList.add(cadUnit);
					}
				}
				//4  "分接箱"
				List<Dydlfjx> listDydlfjx = dydlfjxMapper.selectDeviceList(null, 0, null, lineCode, null, companyId, null, null,0,0,null);
				if (listDydlfjx!=null && listDydlfjx.size()>0) {
					for (int i=0; i<listDydlfjx.size(); i++) {
			          int blockType = Cad_BaseInfo.BLOCK_TYPE_DYDLFJX; //BLOCK_TYPE_DYDLFJX = 8; // 低压电缆分接箱
			          String deviceID = listDydlfjx.get(i).getDeviceid(); 
			          String devicename = listDydlfjx.get(i).getDevicename(); //设备名称
			          String longitude = listDydlfjx.get(i).getLongitude();  //经度
			          String latitude = listDydlfjx.get(i).getLatitude(); //纬度,所属低压线路名称
			          String linename = listDydlfjx.get(i).getLinecode();  //所属低压线路名称--
			          Cad_Point point = Cad_BaseInfo.getxy(longitude, latitude);
			          tmpgps_save(Double.valueOf(longitude).doubleValue(), Double.valueOf(latitude).doubleValue());
			          Cad_Device cadUnit = new Cad_Device();
			          cadUnit.set(deviceID, devicename, preDeviceID, preDeviceName, linename, true, blockType, true, true, point);
			          cadUnit.lineName = linename;
			          cadUnit.hasBX = hasBX(deviceID);
			          resultList.add(cadUnit);
					}
				}
			}
		}
		/*//1"表箱"
		List<Dybx> listDybx = dybxMapper.selectByPreDeviceCode(preDeviceID);
		if (listDybx!=null && listDybx.size()>0) {
			for (int i=0; i<listDybx.size(); i++) {
	          int blockType = Cad_BaseInfo.BLOCK_TYPE_DYBX; //表箱 BLOCK_TYPE_DYBX = 9; // 低压表箱
	          String deviceID = listDybx.get(i).getDeviceid(); 
	          String devicename = listDybx.get(i).getDevicename(); //设备名称
	          String longitude = listDybx.get(i).getLongitude();  //经度
	          String latitude = listDybx.get(i).getLatitude(); //纬度,所属低压线路名称
	          String linename = listDybx.get(i).getLinecode();  //所属低压线路名称--
	          Cad_Point point = Cad_BaseInfo.getxy(longitude, latitude);
	          tmpgps_save(Double.valueOf(longitude).doubleValue(), Double.valueOf(latitude).doubleValue());
	          Cad_Device cadUnit = new Cad_Device();
	          cadUnit.set(deviceID, devicename, preDeviceID, preDeviceName, linename, true, blockType, true, true, point);
	          cadUnit.lineName = linename;
	          resultList.add(cadUnit);
			}
		}
		//2  "剩余电流保护器"
		List<Dysydlbhq> listDysydlbhq = dysydlbhqMapper.selectByPreDeviceCode(preDeviceID);
		if (listDysydlbhq!=null && listDysydlbhq.size()>0) {
			for (int i=0; i<listDysydlbhq.size(); i++) {
	          int blockType = Cad_BaseInfo.BLOCK_TYPE_DYSYDLBHQ; //BLOCK_TYPE_DYSYDLBHQ = 6;// 低压剩余电流保护器
	          String deviceID = listDysydlbhq.get(i).getDeviceid(); 
	          String devicename = listDysydlbhq.get(i).getDevicename(); //设备名称
	          String longitude = listDysydlbhq.get(i).getLongitude();  //经度
	          String latitude = listDysydlbhq.get(i).getLatitude(); //纬度,所属低压线路名称
	          String linename = listDysydlbhq.get(i).getLinecode();  //所属低压线路名称--
	          Cad_Point point = Cad_BaseInfo.getxy(longitude, latitude);
	          tmpgps_save(Double.valueOf(longitude).doubleValue(), Double.valueOf(latitude).doubleValue());
	          Cad_Device cadUnit = new Cad_Device();
	          cadUnit.set(deviceID, devicename, preDeviceID, preDeviceName, linename, true, blockType, true, true, point);
	          cadUnit.lineName = linename;
	          cadUnit.hasBX = hasBX(deviceID);
	          resultList.add(cadUnit);
			}
		}
		//3  "配电箱"
		List<Dypdx> listDypdx = dypdxMapper.selectByPreDeviceCode(preDeviceID);
		if (listDypdx!=null && listDypdx.size()>0) {
			for (int i=0; i<listDypdx.size(); i++) {
	          int blockType = Cad_BaseInfo.BLOCK_TYPE_DYPDX; //BLOCK_TYPE_DYPDX = 7; // 低压配电箱
	          String deviceID = listDypdx.get(i).getDeviceid(); 
	          String devicename = listDypdx.get(i).getDevicename(); //设备名称
	          String longitude = listDypdx.get(i).getLongitude();  //经度
	          String latitude = listDypdx.get(i).getLatitude(); //纬度,所属低压线路名称
	          String linename = listDypdx.get(i).getLinecode();  //所属低压线路名称--
	          Cad_Point point = Cad_BaseInfo.getxy(longitude, latitude);
	          tmpgps_save(Double.valueOf(longitude).doubleValue(), Double.valueOf(latitude).doubleValue());
	          Cad_Device cadUnit = new Cad_Device();
	          cadUnit.set(deviceID, devicename, preDeviceID, preDeviceName, linename, true, blockType, true, true, point);
	          cadUnit.lineName = linename;
	          cadUnit.hasBX = hasBX(deviceID);
	          resultList.add(cadUnit);
			}
		}
		//4  "分接箱"
		List<Dydlfjx> listDydlfjx = dydlfjxMapper.selectByPreDeviceCode(preDeviceID);
		if (listDydlfjx!=null && listDydlfjx.size()>0) {
			for (int i=0; i<listDydlfjx.size(); i++) {
	          int blockType = Cad_BaseInfo.BLOCK_TYPE_DYDLFJX; //BLOCK_TYPE_DYDLFJX = 8; // 低压电缆分接箱
	          String deviceID = listDydlfjx.get(i).getDeviceid(); 
	          String devicename = listDydlfjx.get(i).getDevicename(); //设备名称
	          String longitude = listDydlfjx.get(i).getLongitude();  //经度
	          String latitude = listDydlfjx.get(i).getLatitude(); //纬度,所属低压线路名称
	          String linename = listDydlfjx.get(i).getLinecode();  //所属低压线路名称--
	          Cad_Point point = Cad_BaseInfo.getxy(longitude, latitude);
	          tmpgps_save(Double.valueOf(longitude).doubleValue(), Double.valueOf(latitude).doubleValue());
	          Cad_Device cadUnit = new Cad_Device();
	          cadUnit.set(deviceID, devicename, preDeviceID, preDeviceName, linename, true, blockType, true, true, point);
	          cadUnit.lineName = linename;
	          cadUnit.hasBX = hasBX(deviceID);
	          resultList.add(cadUnit);
			}
		}*/

//		for (int i = 0; i<otsub.length; i++) {
//			ArrayList<Cad_Device>  list = getOtsub(i, deviceName);
//			if (list != null) {
//				resultList.addAll(list);
//			}
//		}
		return resultList;
	}

//	public static final String[] otlay = { "表箱", "剩余电流保护器", "配电箱", "分接箱" };
//	public static final int[][] otsub = { { 9, 9 }, { 11, 6 }, { 15, 7 },{ 16, 8 } };
	//
	public ArrayList<Cad_Device> getOtsub(int subIndex, String parentDevice)
	{
//	    Object[] arrayOfObject1 = new Object[3];
//	    arrayOfObject1[0] = SqlDatabase.Table_names[otsub[subIndex][0]];
//	    arrayOfObject1[1] = SqlDatabase.Table_fields[otsub[subIndex][0]][(-1 + SqlDatabase.Table_fields[otsub[subIndex][0]].length)];
//	    arrayOfObject1[2] = parentDevice;
//	    String str1 = String.format("select 设备名称,经度,纬度,所属低压线路名称 from %1$s where %2$s = '%3$s'", arrayOfObject1);
//	    if ((subIndex == 2) || (subIndex == 3))
//	    {
//	      Object[] arrayOfObject2 = new Object[3];
//	      arrayOfObject2[0] = SqlDatabase.Table_names[otsub[subIndex][0]];
//	      arrayOfObject2[1] = SqlDatabase.Table_fields[otsub[subIndex][0]][(-1 + SqlDatabase.Table_fields[otsub[subIndex][0]].length)];
//	      arrayOfObject2[2] = parentDevice;
//	      str1 = String.format("select 设备名称,经度,纬度 from %1$s where %2$s = '%3$s'", arrayOfObject2);
//	    }
//	    Cursor localCursor = this.sqlbas.OpenRecords(str1);
//	    if ((localCursor != null) && (!localCursor.isClosed()))
//	    {
//	    	 ArrayList<Cad_Device> localArrayList = new  ArrayList<Cad_Device>();
//	    	localCursor.moveToFirst();
//	    	if (localCursor.getCount() > 0)
//	    		localCursor.moveToFirst();
//	        
//	    	while (!localCursor.isAfterLast())
//	        {
//	          String[] otlay = new String[subIndex];
//	          int blockType = otsub[subIndex][1];
//	          String devicename = localCursor.getString(0);
//	          String longitude = localCursor.getString(1);
//	          String latitude = localCursor.getString(2);
//	          String linename = "";
//	          if ((subIndex != 2) && (subIndex != 3)) {
//	        	  linename = localCursor.getString(3);
//	          }
//	          Cad_Point point = Cad_BaseInfo.getxy(longitude, latitude);
//	          tmpgps_save(Double.valueOf(longitude).doubleValue(), Double.valueOf(latitude).doubleValue());
//	          Cad_Device cadUnit = new Cad_Device();
//	          cadUnit.set(devicename, parentDevice, linename, true, blockType, true, true, point);
//	          cadUnit.lineName = linename;
//	          if (otsub[subIndex][0] != 9) {
//	        	  cadUnit.hasBX = hasBX(devicename);
//	          }
//	          localArrayList.add(cadUnit);
//	          localCursor.moveToNext();
//	        }
//	    	localCursor.close();
//	         if (localArrayList.size() <= 0)
//	            return null;
//	         else
//	          return localArrayList;
//	  	}
		return null;
//      if (localCursor.getCount() > 0)
//        localCursor.moveToFirst();
//      while (true)
//      {
//        if (localCursor.isAfterLast())
//        {
//          localCursor.close();
//          if (localArrayList.size() <= 0)
//            break;
//          return localArrayList;
//        }
//        otlay[subIndex];
//        int blockType = otsub[subIndex][1];
//        String str2 = localCursor.getString(0);
//        String str3 = localCursor.getString(1);
//        String str4 = localCursor.getString(2);
//        String str5 = "";
//        if ((subIndex != 2) && (subIndex != 3))
//          str5 = localCursor.getString(3);
//        Ex_dpoint point = Cad_BaseInfo.getxy(str3, str4);
//        tmpgps_save(Double.valueOf(str3).doubleValue(), Double.valueOf(str4).doubleValue());
//        Cad_Device  cadUnit = new Cad_Device ();
//        cadUnit.set(str2, byqId, str5, true, blockType, true, true, point);
//        cadUnit.lineName = str5;
//        if (otsub[subIndex][0] != 9)
//          cadUnit.hasBX = hasBX(str2);
//        localArrayList.add(cadUnit);
//        localCursor.moveToNext();
//      }
//    }
//    return null;
	}

	public ArrayList<Cad_Line> getSlaveBXLinkedLines(ArrayList<Cad_Device> deviceList, Cad_Device device) {
		
		ArrayList<Cad_Point> pointList = getSlaveDYBXPoints(deviceList, device);
		if (pointList == null) {
			return null;
		}
		
		Log(LOG_INFO, "getSlaveBXLinkedLines: 准备生成表箱拓扑线...");

		ArrayList<Cad_BaseDevice>  list1 = new ArrayList<Cad_BaseDevice> ();
		for (int i = 0; i<pointList.size(); i++) {
			Cad_BaseDevice dev = new Cad_BaseDevice();
			Cad_BaseInfo.getDeviceRect(9, dev.drawRect, pointList.get(i));
			dev.linkPoint = pointList.get(i);
			list1.add(dev);
		}
		pointList.clear();
		
		ArrayList<Cad_BaseDevice> list2 = new ArrayList<Cad_BaseDevice>();
		if (Cad_Line._fxotherpts(true, device.linkPoint, list1, list2)==-1){
			list2.clear();
			Cad_Line._fxotherpts(false, device.linkPoint, list1, list2);
		}
		
		
		if (list2.size()>=0) {
			Log(LOG_INFO, "getSlaveBXLinkedLines: 完成表箱拓扑线生成!");

			ArrayList<Cad_Rect> rectList = new ArrayList<Cad_Rect> ();
			if (Cad_Line._mk_rect_line(list2, rectList)) {
				Cad_Rect localEx_drect = (Cad_Rect) rectList.get(-1	+ rectList.size());
				Cad_Point point = device.getlinkpt(localEx_drect.pt2);
				if (point != null) {
					localEx_drect.pt1 = point;
				}
				return getBxLinebyRect(rectList);
			}
			list2.clear();
		}
		return null;
	}

	public ArrayList<Cad_Point> getSlaveDYBXPoints(ArrayList<Cad_Device> deviceList, Cad_Device device) {
		if ((deviceList == null) || (device == null)) {
			return null;
		}
		if (deviceList.indexOf(device) == -1) {
			return null;
		}
		
		ArrayList<Cad_Point> resultList = new ArrayList<Cad_Point>();
		
		for (int i = deviceList.indexOf(device);i<deviceList.size(); i++) {
			Cad_Device dev = deviceList.get(i);
			if (dev.blockType == Cad_BaseInfo.BLOCK_TYPE_DYBX
					&& dev.parentDeviceName.equals(device.deviceName)) {
				resultList.add(dev.linkPoint);
			}
		}
		return resultList;
	}

	public Cad_Device getByq(String byqId) {
		Zsbyq zsbyq = null;
		Pdbyq pdbyq = null;
		String devName;
		Cad_Device cadUnit=null;
		
		boolean isPublicByq = true; //是否是公用变压器，默认都是公变。
		int blockType = Cad_BaseInfo.BLOCK_TYPE_DYGT_ZXGT; //0;
		
		zsbyq = zsbyqList.get(byqId);
		if (zsbyq==null) {
			pdbyq = pdbyqList.get(byqId);
		}
		
		if (zsbyq != null){
			devName = zsbyq.getDevicename();
			if (zsbyq.getUsenature().equals("专用变")) {
				isPublicByq = false;
			}
			blockType = Cad_BaseInfo.BLOCK_TYPE_ZSBYQ; //3
			String Longitude = zsbyq.getLongitude();
			String Latitude = zsbyq.getLatitude();
			Cad_Point point = Cad_BaseInfo.getxy(Longitude, Latitude);
			tmpgps_save(Double.valueOf(Longitude).doubleValue(), Double.valueOf(Latitude).doubleValue());

			cadUnit = new Cad_Device();
			cadUnit.set(byqId, devName, "", "", "", isPublicByq, blockType, true, true,
					point);
			cadUnit.hasBX = hasBX(byqId);
		}else if (pdbyq != null){
			//需要根据视图中压站房设备的视图查询，暂时先从中压杆塔表查询，以便编译通过。
			devName = pdbyq.getDevicename();
			if (pdbyq.getUsenature().equals("专用变")) {
				isPublicByq = false;
			}
			blockType = Cad_BaseInfo.BLOCK_TYPE_PDBYQ; //4
			String Longitude = pdbyq.getLongitude();
			String Latitude = pdbyq.getLatitude();
			Cad_Point point = Cad_BaseInfo.getxy(Longitude, Latitude);
			tmpgps_save(Double.valueOf(Longitude).doubleValue(), Double.valueOf(Latitude).doubleValue());
			cadUnit = new Cad_Device();
			cadUnit.set(byqId, devName, "", "", "", isPublicByq, blockType, true, true,
					point);
			cadUnit.hasBX = hasBX(byqId);
		}
		return cadUnit;

		
//		Object[] arrayOfObject = new Object[2];
//		arrayOfObject[0] = SqlDatabase.Table_names[22];
//		arrayOfObject[1] = pString;
//		String str1 = String.format(
//				"select 设备名称 ,使用性质,变压器类型,经度,纬度 from %1$s where 设备名称 = '%2$s'",
//				arrayOfObject);
//		Cursor localCursor = this.sqlbas.OpenRecords(str1);
//		if ((localCursor != null) && (!localCursor.isClosed())) {
//			localCursor.moveToFirst();
//			if (localCursor.getCount() > 0) {
//				String str2 = localCursor.getString(0);
//				localCursor.getString(1);
//				String str3 = localCursor.getString(2);
//				String str4 = localCursor.getString(3);
//				String str5 = localCursor.getString(4);
//				boolean bool = true;
//              int blockType = 4;
//				if (str3.equals("专用变"))
//					bool = false;
//				if (str3.equals("柱上变压器"))
//					blockType = 3;
//				Cad_Point point = Cad_BaseInfo.getxy(str4, str5);
//				tmpgps_save(Double.valueOf(str4).doubleValue(),
//						Double.valueOf(str5).doubleValue());
//				Cad_Device cadUnit = new Cad_Device();
//				cadUnit.set(str2, "", "", bool, blockType, true, true,
//						point);
//				cadUnit.hasBX = hasBX(pString);
//				localCursor.close();
//				return cadUnit;
//			}
//			localCursor.close();
//		}
	}

	public ArrayList<Cad_Device> getByqAllDrv(String byqId, String byqName) {
		this.tmp_byqjwd = new Cad_Rect();
		this.tmp_byqjwd.pt1.set(140.0D, 60.0D);
		this.tmp_byqjwd.pt2.set(60.0D, 10.0D);
		
		ArrayList<Cad_Device> resultList = new ArrayList<Cad_Device>();
		Cad_Device byqDevice = getByq(byqId);
		
		ArrayList<Cad_Device> subDevList = null;
		if (byqDevice != null) {
			resultList.add(byqDevice);
			subDevList = getAllSubDevice(byqId, byqName);
		}
		
		if (subDevList == null || subDevList.size() <= 0) {
			return resultList;
		}
		resultList.addAll(subDevList);

		ArrayList<Cad_Device> list = new ArrayList<Cad_Device>();

		for (int i=0; i<subDevList.size(); i++){
			Cad_Device unit = subDevList.get(i);
			if (unit != null) {
				ArrayList<Cad_Device> templist = getAllSubDevice(unit.deviceID, unit.deviceName);
				if (templist != null && templist.size()>0) {
					list.addAll(templist);
				}
			}
		}
		subDevList.clear();
		if (list.size() > 0) {
			resultList.addAll(list);
		}
		
		return resultList;
		
//		do
//			if (localArrayList2 == null) {
//				return localArrayList1;
//				return null;
//			}
//		while (localArrayList2.size() <= 0);
//		
//		
//		localArrayList1.addAll(localArrayList2);
//		int i = 0;
//		label110: if (i >= localArrayList2.size()) {
//			localArrayList2.clear();
//			if (localArrayList3.size() <= 0)
//				break label196;
//			localArrayList2.addAll(localArrayList3);
//		}
//		while (true) {
//			localArrayList3.clear();
//			break;
//			Cad_Device  unit2 = (Cad_Device ) localArrayList2.get(i);
//			if (unit2 != null) {
//				ArrayList localArrayList4 = getAllSubDevice(unit2.deviceName);
//				if (localArrayList4 != null)
//					localArrayList3.addAll(localArrayList4);
//			}
//			i++;
//			break label110;
//			label196: localArrayList2 = null;
//		}
	}

	public ArrayList<DXDL> getByqAllDxDl(String byqId, String byqName, boolean isDYDX) {
		ArrayList<XL> xlList = getXL(byqId);
		//ArrayList localArrayList1 = getXL(byqId);
		if (xlList == null || xlList.size()<=0 ) {
			return null;
		}
		ArrayList<DXDL>  resultList = new ArrayList<DXDL> ();
		
		for (int i=0; i<xlList.size(); i++) {
			ArrayList<DXDL> list = getxl_dxdl(xlList.get(i).id, xlList.get(i).name, isDYDX);
			if (list != null) {
				resultList.addAll(list);
			}
		}
		for (int j=0; j<resultList.size(); j++) {
			resultList.get(j).byqid = byqId;
			resultList.get(j).byqname = byqName;
		}
		return resultList; 
		
		//		if (localArrayList1 != null) {
//			ArrayList localArrayList2 = new ArrayList();
//			int i = 0;
//			if (i >= localArrayList1.size()) {
//				if (localArrayList2.size() <= 0)
//					;
//			} else
//				for (int j = 0;; j++) {
//					if (j >= localArrayList2.size()) {
//						return localArrayList2;
//						ArrayList localArrayList3 = getxl_dxdl(
//								((XL) localArrayList1.get(i)).name,
//								pBoolean);
//						if (localArrayList3 != null)
//							localArrayList2.addAll(localArrayList3);
//						i++;
//						break;
//					}
//					((DXDL) localArrayList2.get(j)).byq = pString;
//				}
//		}
//		return null;
	}
	

	public ArrayList<Cad_Line> getByqAllDxDl_Lines(String byqId, String byqName, ArrayList<Cad_Device> deviceList) {
		if (deviceList == null)
			return null;
		
		ArrayList<Cad_Line> lineList = new ArrayList<Cad_Line>();
		
		ArrayList<Cad_Line> list=null;
		list = getAllDxdl_Lines(getByqAllDxDl(byqId, byqName, true), deviceList, true);
		if (list != null)
			 lineList.addAll(list);
		
		ArrayList<Cad_Line> list2=null;
		list2 = getAllDxdl_Lines(getByqAllDxDl(byqId, byqName, false), deviceList, false);
		if (list2 != null)
			lineList.addAll(list2);
		
		return lineList;
	}
	
	//根据关联设备查找杆塔 2015/1/20
	public ArrayList<Cad_Device> getDYGT(String byqID, String byqName) {
		ArrayList<Cad_Device> resultList = new ArrayList<Cad_Device>();
		List<Dyxl> listDyxl = dyxlMapper.selectByBYQ(byqID);
		if (listDyxl==null || listDyxl.size()<=0) {
			return null;
		}
		
		for (int j=0; j<listDyxl.size(); j++){
			Dyxl dyxl = listDyxl.get(j);
			String lineCode = dyxl.getDeviceid();
			List<Dygt> listDygt = dygtMapper.selectByLineCode(lineCode);
			if (listDygt !=null && listDygt.size()>0) {
				for (int i=0; i<listDygt.size(); i++){
					Dygt dygt = listDygt.get(i);
					int blockType = Cad_BaseInfo.BLOCK_TYPE_DYGT_NZGT; //BLOCK_TYPE_DYGT_NZGT = 1; // 耐张杆塔
					if (dygt.getTowernature().contains("直线")){
						blockType = Cad_BaseInfo.BLOCK_TYPE_DYGT_ZXGT;  //BLOCK_TYPE_DYGT_ZXGT = 0; // 直线杆塔
					}
					if (dygt.getIsterminal().contains("是")){
						//
					}
					boolean bool = hasdrv(dygt);
					Cad_Point point = Cad_BaseInfo.getxy(dygt.getLongitude(), dygt.getLatitude());
					tmpgps_save(Double.valueOf(dygt.getLongitude()).doubleValue(),
							Double.valueOf(dygt.getLatitude()).doubleValue());
					Cad_Device cadUnit = new Cad_Device();
					
					String preDeviceName = null;
					if(dygt.getPredevicecode()==null || dygt.getPredevicecode().isEmpty()){
						preDeviceName = byqName;
					}else{
						try {
							preDeviceName = businessCommonService.getDeviceName(dygt.getPredevicetype().longValue(),dygt.getPredevicecode());
						} catch (PwtzDatabaseException e) {
							e.printStackTrace();
						}
					}
					
					cadUnit.set(dygt.getDeviceid(), dygt.getDevicename(),
							byqID, preDeviceName, dygt.getLinecode(), false, blockType,
							//bool
							true
							, true, point);
					cadUnit.hasBX = hasBX(dygt.getDeviceid());
					resultList.add(cadUnit);				
				}
			}
		}
		return resultList;
//		Object[] arrayOfObject = new Object[3];
//		arrayOfObject[0] = SqlDatabase.Table_names[2];  //低压杆塔
//		arrayOfObject[1] = SqlDatabase.Table_fields[2][(-1 + SqlDatabase.Table_fields[2].length)]; //设备编码
//		arrayOfObject[2] = pString;
//		String str1 = String
//				.format("select 杆塔编号,所属低压线路名称,杆塔性质,是否终端,经度,纬度 from %1$s where %2$s = '%3$s' ",
//						arrayOfObject);
//		Cursor localCursor = this.sqlbas.OpenRecords(str1);
//		if ((localCursor != null) && (!localCursor.isClosed())) {
//			localCursor.moveToFirst();
//			if (localCursor.getCount() > 0) {
//				ArrayList localArrayList = new ArrayList();
//				localCursor.moveToFirst();
//				if (localCursor.isAfterLast())
//					return localArrayList;
//				String str2 = localCursor.getString(0);
//				String str3 = localCursor.getString(1);
//				String str4 = localCursor.getString(2);
//				String str5 = localCursor.getString(3);
//				String str6 = localCursor.getString(4);
//				String str7 = localCursor.getString(5);
//				int blockType = 1;
//				if (str4.equals("直线"))
//					blockType = 0;
//				if (str5.equals("是"))
//					;
//				for (boolean bool = true;; bool = hasdrv(str2)) {
//					Cad_Point point = Cad_BaseInfo.getxy(str6, str7);
//					tmpgps_save(Double.valueOf(str6).doubleValue(), Double
//							.valueOf(str7).doubleValue());
//					Cad_Device cadUnit = new Cad_Device();
//					cadUnit.set(str2, pString, str3, false, blockType, bool,
//							true, point);
//					cadUnit.hasBX = hasBX(str2);
//					localArrayList.add(cadUnit);
//					localCursor.moveToNext();
//					break;
//				}
//			}
//			localCursor.close();
//		}
//		return null;
	}

	public Cad_Device getNearUnit(ArrayList<Cad_Device> pArrayList,
			Cad_Device pEx_Unit) {
		Cad_Device cadUnit = null;
		if ((pArrayList == null) || (pEx_Unit == null))
			return null;

		double d1 = -1.0D;
		for (int i = 0; i < pArrayList.size(); i++)
			if ((((Cad_Device) pArrayList.get(i)).lineName.equals(pEx_Unit.lineName))
					&& (!isMaster_Slave(pArrayList, pEx_Unit, (Cad_Device) pArrayList.get(i)))) {
				double d2 = Cad_Line._getjl(pEx_Unit.linkPoint, ((Cad_Device) pArrayList.get(i)).linkPoint);
				if ((d1 == -1.0D) || (d1 > d2)) {
					d1 = d2;
					cadUnit = (Cad_Device) pArrayList.get(i);
					break;
				}
			}
		return cadUnit;
	}

	public ArrayList<XL> getXL(String byqId) {
		
		ArrayList<XL> xlList = new ArrayList<XL>();
		
		//Pdbyq
		List<Dyxl> list = dyxlMapper.selectByBYQ(byqId);
		
		if (list != null && list.size()>0){
			for (int i=0; i<list.size(); i++) {
				XL xl = new XL();
				xl.id = list.get(i).getDeviceid();
				xl.name = list.get(i).getDevicename();
				xl.byq = byqId;
				xl.byqId = byqId;
				xlList.add(xl);
			}
		}

		return xlList;
	}

	public ArrayList<DXDL> getxl_dxdl(String lineCode, String lineName, boolean isDYDX) {
		ArrayList<DXDL> resultList = new ArrayList<DXDL>();
		//T: 低压导线 F:低压电缆
		if (isDYDX) {
			List<Dydx> listDydx = dydxMapper.selectDeviceList(null, 0, null, lineCode, null, null, null, null, 0, 0);
			if (listDydx==null || listDydx.size()<=0) {
				return null;
			}
			for (int i=0; i<listDydx.size(); i++) {
				Dydx dydx = listDydx.get(i);
				DXDL local_dxdl = new DXDL();
				local_dxdl.name = dydx.getDevicename();
				local_dxdl.startdrv = dydx.getStarttowercode();
				local_dxdl.enddrv = dydx.getEndtowercode();
				local_dxdl.linecode = lineCode;
				local_dxdl.linename = lineName;
				resultList.add(local_dxdl);
			}
		}
		else{
			List<Dydl> listDydl = dydlMapper.selectDeviceList(null, 0l, null, 0, null, lineCode, null, null, 0, 0);
			if (listDydl==null || listDydl.size()<=0) {
				return null;
			}
			for (int i=0; i<listDydl.size(); i++) {
				Dydl dydl = listDydl.get(i);
				DXDL local_dxdl = new DXDL();
				local_dxdl.name = dydl.getDevicename();
				local_dxdl.startdrv = dydl.getStartdevicecode();
				local_dxdl.enddrv = dydl.getEnddevicecode();
				local_dxdl.linecode = lineCode;
				local_dxdl.linename = lineName;
				resultList.add(local_dxdl);
			}		
		}
		
		return resultList;
//		Object[] arrayOfObject1 = new Object[2];
//		arrayOfObject1[0] = SqlDatabase.Table_names[1]; //低压导线
//		arrayOfObject1[1] = lineName;
//		String str = String.format(
//				"select 设备名称,起始杆塔,终止杆塔 from %1$s where 所属低压线路名称 = '%2$s'",
//				arrayOfObject1);
//		if (!isDYDX) {
//			Object[] arrayOfObject2 = new Object[2];
//			arrayOfObject2[0] = SqlDatabase.Table_names[12]; //低压电缆段
//			arrayOfObject2[1] = lineName;
//			str = String
//					.format("select 设备名称,起点连接设备,终点连接设备 from %1$s where 所属低压线路名称 = '%2$s'",
//							arrayOfObject2);
//		}
//		Cursor localCursor = this.sqlbas.OpenRecords(str);
//		if ((localCursor != null) && (!localCursor.isClosed())) {
//			ArrayList localArrayList = new ArrayList();
//			localCursor.moveToFirst();
//			if (localCursor.getCount() > 0)
//				localCursor.moveToFirst();
//			while (true) {
//				if (localCursor.isAfterLast()) {
//					localCursor.close();
//					if (localArrayList.size() <= 0)
//						break;
//					return localArrayList;
//				}
//				DXDL local_dxdl = new DXDL();
//				local_dxdl.name = localCursor.getString(0);
//				local_dxdl.startdrv = localCursor.getString(1);
//				local_dxdl.enddrv = localCursor.getString(2);
//				local_dxdl.xl = lineName;
//				localArrayList.add(local_dxdl);
//				localCursor.moveToNext();
//			}
//		}
//		return null;
	}

	public boolean hasBX(String deviceID) {
		boolean result = false;
		List<Dybx> list = dybxMapper.selectByPreDeviceCode(deviceID);
		if (list !=null && list.size()>0) {
			result = true;
		}
		return result; 
		
//		Object[] arrayOfObject = new Object[4];
//		arrayOfObject[0] = SqlDatabase.Table_fields[9][0];
//		arrayOfObject[1] = SqlDatabase.Table_names[9]; //"低压表箱"
//		arrayOfObject[2] = SqlDatabase.Table_fields[9][(-1 + SqlDatabase.Table_fields[9].length)];
//		arrayOfObject[3] = deviceName;
//		String str = String.format("select %1$s from %2$s where %3$s = '%4$s'",
//				arrayOfObject);
//		Cursor localCursor = this.sqlbas.OpenRecords(str);
//		boolean bool1 = false;
//		if (localCursor != null) {
//			boolean bool2 = localCursor.isClosed();
//			bool1 = false;
//			if (!bool2) {
//				localCursor.moveToFirst();
//				int i = localCursor.getCount();
//				bool1 = false;
//				if (i > 0)
//					bool1 = true;
//				localCursor.close();
//			}
//		}
//		return bool1;
	}

	//判断低压杆塔是否有柱上设备
	public boolean hasdrv(Dygt dygt) {
		boolean result = false;
		
		//低压柱上开关     T_DYZSKG
		List<Dyzskg> list1 = dyzskgMapper.selectByDytg(dygt.getDeviceid(), "", dygt.getCompanyid(), 0, 0);
		if (list1 != null && list1.size()>0) {
			result = true;
			return result;	}

		//低压柱熔断器     T_DYZRDQ
		List<Dyzrdq> list2 = dyzrdqMapper.selectByDytg(dygt.getDeviceid(), "", dygt.getCompanyid(), 0, 0);
		if (list2 != null && list2.size()>0) {
			result = true;
			return result;
		}
		 
		//低压柱上熔丝箱  T_DYRSX
		List<Dyrsx> list3 = dyrsxMapper.selectByDytg(dygt.getDeviceid(), "", dygt.getCompanyid(), 0, 0);
		if (list3 != null && list3.size()>0) {
			result = true;
			return result;
		}
		
		//低压柱上电容器  T_DYZSDRQ	
		List<Dyzsdrq> list4 = dyzsdrqMapper.selectByDytg(dygt.getDeviceid(), "", dygt.getCompanyid(), 0, 0);
		if (list4 != null && list4.size()>0) {
			result = true;
			return result;
		}
		
		//低压柱上无功补偿装置  T_DYWGBCZZ
		List<Dywgbczz> list5 = dywgbczzMapper.selectByDytg(dygt.getDeviceid(), "", dygt.getCompanyid(), 0, 0);
		if (list5 != null && list5.size()>0) {
			result = true;
			return result;
		}
		
		//低压柱上避雷器 T_DYZSBLQ
		List<Dyzsblq> list6 = dyzsblqMapper.selectByDytg(dygt.getDeviceid(), "", dygt.getCompanyid(), 0, 0);
		if (list6 != null && list6.size()>0) {
			result = true;
			return result;
		}

		//判断是否关联表箱
		result = hasBX(dygt.getDeviceid()); 
		
		return result;
		
//		for (int i = 0;; i++) {
//			if ((i >= subDeviceOfDYGT.length) || (bool)) {
//				if (!bool)
//					bool = hasBX(pString);
//				return bool;
//			}
//			Object[] arrayOfObject = new Object[3];
//			arrayOfObject[0] = SqlDatabase.Table_fields[subDeviceOfDYGT[i]][0];
//			arrayOfObject[1] = SqlDatabase.Table_names[subDeviceOfDYGT[i]];
//			arrayOfObject[2] = pString;
//			String str = String.format(
//					"select %1$s from %2$s where 所属杆塔 = '%3$s'", arrayOfObject);
//			Cursor localCursor = this.sqlbas.OpenRecords(str);
//			if ((localCursor != null) && (!localCursor.isClosed())) {
//				localCursor.moveToFirst();
//				if (localCursor.getCount() > 0)
//					bool = true;
//				localCursor.close();
//			}
//		}
	}

	public boolean isMaster_Slave(ArrayList<Cad_Device> pArrayList,
			Cad_Device pEx_Unit1, Cad_Device pEx_Unit2) {
		boolean bool=false;
		if ((pArrayList == null) || (pEx_Unit1 == null)
				|| (pEx_Unit2 == null)){
			bool = false;
		}
		int i = pArrayList.indexOf(pEx_Unit1);
		int j = pArrayList.indexOf(pEx_Unit2);
		int k = Math.max(i, j);
		int m = Math.min(i, j);
		bool = false;
		Cad_Device unit1 = new Cad_Device();
		Cad_Device unit2;
		if (i > j) {
			unit2 = pEx_Unit1;
			unit1.deviceName = unit2.parentDeviceName;
		}
		while (true) {
			if ((m <= k) || (bool)){
				break;
			}
			m = pArrayList.indexOf(unit1);
			if (m < k) {
				k = 0;
				continue;
//				unit2 = pEx_Unit2;
//				break;
			}
			if (m == k)
				bool = true;
			else
				unit1.deviceName = ((Cad_Device) pArrayList.get(m)).parentDeviceName;
		}

		
		return bool;

//		boolean bool;
//		if ((pArrayList == null) || (pEx_Unit1 == null)
//				|| (pEx_Unit2 == null))
//			bool = false;
//		label152: while (true) {
//			return bool;
//			int i = pArrayList.indexOf(pEx_Unit1);
//			int j = pArrayList.indexOf(pEx_Unit2);
//			int k = Math.max(i, j);
//			int m = Math.min(i, j);
//			bool = false;
//			Cad_Device  unit1 = new Cad_Device ();
//			Cad_Device  unit2;
//			if (i > j) {
//				unit2 = pEx_Unit1;
//				unit1.deviceName = unit2.parentDeviceName;
//			}
//			while (true) {
//				if ((m <= k) || (bool))
//					break label152;
//				m = pArrayList.indexOf(unit1);
//				if (m < k) {
//					k = 0;
//					continue;
//					unit2 = pEx_Unit2;
//					break;
//				}
//				if (m == k)
//					bool = true;
//				else
//					unit1.deviceName = ((Cad_Device ) pArrayList.get(m)).parentDeviceName;
//			}
//		}
	}

	public void tmpgps_save(double d1, double d2) {
		if (this.tmp_byqjwd == null)
			return;
		this.tmp_byqjwd.pt1.x = Math.min(d1, this.tmp_byqjwd.pt1.x);
		this.tmp_byqjwd.pt1.y = Math.min(d2, this.tmp_byqjwd.pt1.y);
		this.tmp_byqjwd.pt2.x = Math.max(d1, this.tmp_byqjwd.pt2.x);
		this.tmp_byqjwd.pt2.y = Math.max(d2, this.tmp_byqjwd.pt2.y);
	}

	public class DXDL {
		String byqid = "";
		String byqname = "";
		String id = "";
		String name = "";
		String enddrv = "";
		String startdrv = "";
		String linecode = "";
		String linename = "";

		public DXDL() {
		}

		public boolean equals(Object pObject) {
			if ((pObject instanceof DXDL))
				return ((DXDL) pObject).id.equals(this.id) && ((DXDL) pObject).name.equals(this.name) ;
			return false;
		}
	}

	public class XL {
		public String byq = "";
		public String byqId = "";
		public String id = "";
		public String name = "";

		public XL() {
		}

		public boolean equals(Object pObject) {
			if ((pObject instanceof XL))
				return ((XL) pObject).id.equals(this.id) && ((XL) pObject).name.equals(this.name);
			return false;
		}
	}
	
	public class ByqTpt {
		double ratio = 10.0D;
		ArrayList<Cad_Line> lineList = new ArrayList<Cad_Line> ();
		ArrayList<Cad_Device> deviceList = new ArrayList<Cad_Device> ();
		Cad_Rect mapRect = new Cad_Rect();
		Cad_Rect deviceLineRect = new Cad_Rect();
		Cad_Rect rect = new Cad_Rect();

		public ByqTpt() {
		}

		public boolean calcDevLineRect() {
			if ((this.deviceList == null) || (this.lineList == null) || (this.deviceLineRect == null)) {
				return false;
			}

			if (this.deviceList.size() == 0) {
				return false;
			}
			
			this.deviceLineRect.set(this.deviceList.get(0).drawRect);
			
			for (int i=0; i<this.deviceList.size(); i++){
				Cad_Device cadUnit = this.deviceList.get(i);
				this.deviceLineRect.pt1.x = Math.min(this.deviceLineRect.pt1.x, cadUnit.drawRect.left);
				this.deviceLineRect.pt1.y = Math.min(this.deviceLineRect.pt1.y, cadUnit.drawRect.bottom);
				this.deviceLineRect.pt2.x = Math.max(this.deviceLineRect.pt2.x, cadUnit.drawRect.right);
				this.deviceLineRect.pt2.y = Math.max(this.deviceLineRect.pt2.y, cadUnit.drawRect.top);
				}
			for (int j=0; j<this.lineList.size(); j++){
				Cad_Line cadLine = this.lineList.get(j);
				this.deviceLineRect.pt1.x = Math.min(this.deviceLineRect.pt1.x, cadLine.rect.left);
				this.deviceLineRect.pt1.y = Math.min(this.deviceLineRect.pt1.y, cadLine.rect.bottom);
				this.deviceLineRect.pt2.x = Math.max(this.deviceLineRect.pt2.x, cadLine.rect.right);
				this.deviceLineRect.pt2.y = Math.max(this.deviceLineRect.pt2.y, cadLine.rect.top);
			}
			return true;
			
//			int i = 0;
//			if (i >= this.deviceList.size())
//				;
//			for (int j = 0;; j++) {
//				if (j >= this.lineList.size()) {
//					return true;
//					Cad_Device  cadUnit = (Cad_Device ) this.deviceList.get(i);
//					this.deviceLineRect.pt1.x = Math
//							.min(this.deviceLineRect.pt1.x,
//									cadUnit.drawRect.left);
//					this.deviceLineRect.pt1.y = Math.min(
//							this.deviceLineRect.pt1.y,
//							cadUnit.drawRect.bottom);
//					this.deviceLineRect.pt2.x = Math.max(
//							this.deviceLineRect.pt2.x,
//							cadUnit.drawRect.right);
//					this.deviceLineRect.pt2.y = Math.max(
//							this.deviceLineRect.pt2.y, cadUnit.drawRect.top);
//					i++;
//					break;
//				}
//				Cad_Line cadLine = (Cad_Line) this.lineList.get(j);
//				this.deviceLineRect.pt1.x = Math.min(this.deviceLineRect.pt1.x,
//						cadLine.rect.left);
//				this.deviceLineRect.pt1.y = Math.min(this.deviceLineRect.pt1.y,
//						cadLine.rect.bottom);
//				this.deviceLineRect.pt2.x = Math.max(this.deviceLineRect.pt2.x,
//						cadLine.rect.right);
//				this.deviceLineRect.pt2.y = Math.max(this.deviceLineRect.pt2.y,
//						cadLine.rect.top);
//			}
		}

		public boolean moveof(Cad_Point pEx_dpoint) {
			if ((this.deviceList == null) || (this.lineList == null)
					|| (this.rect == null) || (this.deviceLineRect == null))
				return false;
			
			for(int i=0; i<this.deviceList.size(); i++){
				this.deviceList.get(i).moveof(pEx_dpoint);
			}
			for (int j=0; j<this.lineList.size(); j++){
				 this.lineList.get(j).moveof(pEx_dpoint);
			}
			this.mapRect.moveof(pEx_dpoint);
			this.rect.moveof(pEx_dpoint);
			this.deviceLineRect.moveof(pEx_dpoint);
			return true;
			
//			int i = 0;
//			if (i >= this.deviceList.size())
//				;
//			for (int j = 0;; j++) {
//				if (j >= this.lineList.size()) {
//					this.mapRect.moveof(pEx_dpoint);
//					this.rect.moveof(pEx_dpoint);
//					this.deviceLineRect.moveof(pEx_dpoint);
//					return true;
//					((Cad_Device ) this.deviceList.get(i)).moveof(pEx_dpoint);
//					i++;
//					break;
//				}
//				((Cad_Line) this.lineList.get(j)).moveof(pEx_dpoint);
//			}
		}
	}
}

